Decomposing Dates and Times Using Formatting Functions : Date Format « Date Time « SQL / MySQL






Decomposing Dates and Times Using Formatting Functions

      
mysql>
mysql> CREATE TABLE datetime_val
    -> (
    ->  dt      DATETIME
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql>
mysql>
mysql> INSERT INTO datetime_val (dt) VALUES('1970-01-01 00:00:00');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO datetime_val (dt) VALUES('1987-03-05 12:30:15');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO datetime_val (dt) VALUES('1999-12-31 09:00:00');
Query OK, 1 row affected (0.00 sec)

mysql> INSERT INTO datetime_val (dt) VALUES('2000-06-04 15:45:30');
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> SELECT * FROM datetime_val;
+---------------------+
| dt                  |
+---------------------+
| 1970-01-01 00:00:00 |
| 1987-03-05 12:30:15 |
| 1999-12-31 09:00:00 |
| 2000-06-04 15:45:30 |
+---------------------+
4 rows in set (0.00 sec)

mysql>
mysql> SELECT dt,
    -> DATE_FORMAT(dt,'%Y') AS year,
    -> DATE_FORMAT(dt,'%d') AS day,
    -> TIME_FORMAT(dt,'%H') AS hour,
    -> TIME_FORMAT(dt,'%s') AS second
    -> FROM datetime_val;
+---------------------+------+------+------+--------+
| dt                  | year | day  | hour | second |
+---------------------+------+------+------+--------+
| 1970-01-01 00:00:00 | 1970 | 01   | 00   | 00     |
| 1987-03-05 12:30:15 | 1987 | 05   | 12   | 15     |
| 1999-12-31 09:00:00 | 1999 | 31   | 09   | 00     |
| 2000-06-04 15:45:30 | 2000 | 04   | 15   | 30     |
+---------------------+------+------+------+--------+
4 rows in set (0.00 sec)

mysql>
mysql> drop table datetime_val;
Query OK, 0 rows affected (0.00 sec)

mysql>

   
    
    
    
    
    
  








Related examples in the same category

1.Format a date 1
2.Format a date 2
3.Format a date 3
4.Format a date 4
5.Format a date 5
6.Date Symbols in DATE_FORMAT, TIME_FORMAT, and FROM_UNIXTIME
7.Time Symbols in DATE_FORMAT, TIME_FORMAT, and FROM_UNIXTIME
8.Common used flags for DATE_FORMAT()
9.DATE_FORMAT Specifiers
10.DATE_FORMAT function
11.SELECT DATE_FORMAT('2005-12-31', '%M %d %Y')
12.SELECT DATE_FORMAT('2005-12-31', '%D of %M')
13.To return the birthdays of all staff in the format MM/DD/YYYY, use the DATE_FORMAT() function
14.SELECT DATE_FORMAT("2010-08-30 21:19:58", "%W %M %d %Y");
15.SELECT DATE_FORMAT("2010-08-30 21:19:58", "%a. %b %e, '%y");
16.SELECT DATE_FORMAT("2010-08-30 21:19:58", "%m-%e-%Y %l:%i%p");
17.SELECT DATE_FORMAT("2010-08-30 21:19:58", "%m-%e-%Y %h:%i%s%p %W");
18.SELECT DATE_FORMAT("2010-08-30 21:19:58", "%M %D,%Y %k:%i CST %W");
19.Formatting Dates and Times
20.SELECT @@GLOBAL.DATETIME_FORMAT
21.SET @@SESSION.DATETIME_FORMAT = DEFAULT
22.Telling MySQL How to Display Dates or Times
23.The DATE and TIME data types use the following format:
24.Present a date differently than in CCYY-MM-DD format or present a time without the seconds part
25.Calculating One Date from Another by Substring Replacement
26.The string replacement technique can be used to produce dates with a specific position within the calendar year
27.For Christmas, replace the month and day with 12 and 25:
28.%a is the abbreviated weekday name, %D is the day of month with the suffix attached, %b is the abbreviated mon