ProblemYou want to obtain just a part of a date or a time. SolutionYou have several options:
DiscussionThe following discussion shows different ways to extract parts of temporal values. Decomposing dates or times using component-extraction functionsMySQL includes many functions for extracting date or time parts from temporal values. For example, DATE( ) or TIME( ) extracts the date or time part of temporal values: mysql> SELECT dt, DATE(dt), TIME(dt) FROM datetime_val; +---------------------+------------+----------+ | dt | DATE(dt) | TIME(dt) | +---------------------+------------+----------+ | 1970-01-01 00:00:00 | 1970-01-01 | 00:00:00 | | 1987-03-05 12:30:15 | 1987-03-05 | 12:30:15 | | 1999-12-31 09:00:00 | 1999-12-31 | 09:00:00 | | 2000-06-04 15:45:30 | 2000-06-04 | 15:45:30 | +---------------------+------------+----------+ Some of the other component-extraction functions are shown in the following list; consult the MySQL Reference Manual for a complete list. The date-related functions work with DATE, DATETIME, or TIMESTAMP values. The time-related functions work with TIME, DATETIME, or TIMESTAMP values.
Here's an example: mysql> SELECT dt, -> YEAR(dt), DAYOFMONTH(dt), -> HOUR(dt), SECOND(dt) -> FROM datetime_val; +---------------------+----------+----------------+----------+------------+ | dt | YEAR(dt) | DAYOFMONTH(dt) | HOUR(dt) | SECOND(dt) | +---------------------+----------+----------------+----------+------------+ | 1970-01-01 00:00:00 | 1970 | 1 | 0 | 0 | | 1987-03-05 12:30:15 | 1987 | 5 | 12 | 15 | | 1999-12-31 09:00:00 | 1999 | 31 | 9 | 0 | | 2000-06-04 15:45:30 | 2000 | 4 | 15 | 30 | +---------------------+----------+----------------+----------+------------+ Functions such as YEAR( ) or DAYOFMONTH( ) extract values that have an obvious correspondence to a substring of the temporal value to which you apply them. Other date component-extraction functions provide access to values that have no such correspondence. One is the day-of-year value: mysql> SELECT d, DAYOFYEAR(d) FROM date_val; +------------+--------------+ | d | DAYOFYEAR(d) | +------------+--------------+ | 1864-02-28 | 59 | | 1900-01-15 | 15 | | 1987-03-05 | 64 | | 1999-12-31 | 365 | | 2000-06-04 | 156 | +------------+--------------+ Another is the day of the week, which can be obtained either by name or by number:
EXtrACT( ) is another function for obtaining individual parts of temporal values: mysql> SELECT dt, -> EXTRACT(DAY FROM dt), -> EXTRACT(HOUR FROM dt) -> FROM datetime_val; +---------------------+----------------------+-----------------------+ | dt | EXTRACT(DAY FROM dt) | EXTRACT(HOUR FROM dt) | +---------------------+----------------------+-----------------------+ | 1970-01-01 00:00:00 | 1 | 0 | | 1987-03-05 12:30:15 | 5 | 12 | | 1999-12-31 09:00:00 | 31 | 9 | | 2000-06-04 15:45:30 | 4 | 15 | +---------------------+----------------------+-----------------------+ The keyword indicating what to extract from the value should be a unit specifier such as YEAR, MONTH, DAY, HOUR, MINUTE, or SECOND. (Check the MySQL Reference Manual for the full list.) Note that each unit specifier is given in singular form, not plural.
Decomposing dates or times using formatting functionsThe DATE_FORMAT( ) and TIME_FORMAT( ) functions reformat date and time values. By specifying appropriate format strings, you can extract individual parts of temporal values: 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 | +---------------------+------+------+------+--------+ Formatting functions enable you to extract more than one part of a value. For example, to extract the entire date or time from DATETIME values, do this: mysql> SELECT dt, -> DATE_FORMAT(dt,'%Y-%m-%d') AS 'date part', -> TIME_FORMAT(dt,'%T') AS 'time part' -> FROM datetime_val; +---------------------+------------+-----------+ | dt | date part | time part | +---------------------+------------+-----------+ | 1970-01-01 00:00:00 | 1970-01-01 | 00:00:00 | | 1987-03-05 12:30:15 | 1987-03-05 | 12:30:15 | | 1999-12-31 09:00:00 | 1999-12-31 | 09:00:00 | | 2000-06-04 15:45:30 | 2000-06-04 | 15:45:30 | +---------------------+------------+-----------+ One advantage of using formatting functions is that you can display the extracted values in a different form from that in which they're present in the original values. If you want to present a date differently from CCYY-MM-DD format or present a time without the seconds part, that's easy to do: mysql> SELECT ts, -> DATE_FORMAT(ts,'%M %e, %Y') AS 'descriptive date', -> TIME_FORMAT(ts,'%H:%i') AS 'hours/minutes' -> FROM timestamp_val; +---------------------+-------------------+---------------+ | ts | descriptive date | hours/minutes | +---------------------+-------------------+---------------+ | 1970-01-01 00:00:00 | January 1, 1970 | 00:00 | | 1987-03-05 12:30:15 | March 5, 1987 | 12:30 | | 1999-12-31 09:00:00 | December 31, 1999 | 09:00 | | 2000-06-04 15:45:30 | June 4, 2000 | 15:45 | +---------------------+-------------------+---------------+ Decomposing dates or times using string functionsThe discussion in this section thus far has shown how to extract components of temporal values using functions such as YEAR( ), MONTH( ), and DATE_FORMAT( ). If you pass a date or time value to a string function, MySQL treats it as a string, so string functions provide another way to decompose temporal values. This means that you can extract pieces of temporal values by using functions such as LEFT( ) or MID( ) to pull out substrings: mysql> SELECT dt, -> LEFT(dt,4) AS year, -> MID(dt,9,2) AS day, -> RIGHT(dt,2) AS second -> FROM datetime_val; +---------------------+------+------+--------+ | dt | year | day | second | +---------------------+------+------+--------+ | 1970-01-01 00:00:00 | 1970 | 01 | 00 | | 1987-03-05 12:30:15 | 1987 | 05 | 15 | | 1999-12-31 09:00:00 | 1999 | 31 | 00 | | 2000-06-04 15:45:30 | 2000 | 04 | 30 | +---------------------+------+------+--------+ You can obtain the entire date or time part from DATETIME or TIMESTAMP values using string-extraction functions such as LEFT( ) or RIGHT( ): mysql> SELECT dt, -> LEFT(dt,10) AS date, -> RIGHT(dt,8) AS time -> FROM datetime_val; +---------------------+------------+----------+ | dt | date | time | +---------------------+------------+----------+ | 1970-01-01 00:00:00 | 1970-01-01 | 00:00:00 | | 1987-03-05 12:30:15 | 1987-03-05 | 12:30:15 | | 1999-12-31 09:00:00 | 1999-12-31 | 09:00:00 | | 2000-06-04 15:45:30 | 2000-06-04 | 15:45:30 | +---------------------+------------+----------+ mysql> SELECT ts, -> LEFT(ts,10) AS date, -> RIGHT(ts,8) AS time -> FROM timestamp_val; +---------------------+------------+----------+ | ts | date | time | +---------------------+------------+----------+ | 1970-01-01 00:00:00 | 1970-01-01 | 00:00:00 | | 1987-03-05 12:30:15 | 1987-03-05 | 12:30:15 | | 1999-12-31 09:00:00 | 1999-12-31 | 09:00:00 | | 2000-06-04 15:45:30 | 2000-06-04 | 15:45:30 | +---------------------+------------+----------+ Decomposition of temporal values with string functions is subject to a couple of constraints that component extraction and reformatting functions are not bound by:
|