|
MySQL Cookbook Authors: DuBois P. Published year: 2005 Pages: 112/412 |
5.6 Decomposing Dates or Times Using Component-Extraction Functions5.6.1 ProblemYou want to obtain just a part of a date or a time. 5.6.2 SolutionInvoke a function specifically intended for extracting part of a temporal value, such as MONTH( ) or MINUTE( ) . For obtaining single components of temporal values, these functions are faster than using DATE_FORMAT( ) for the equivalent operation. 5.6.3 DiscussionMySQL includes many functions for extracting date or time parts from temporal values. Some of these 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 date values. Some date 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:
Another way to obtain individual parts of temporal values is to use the EXTRACT( ) function: 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 should be a unit specifier such as YEAR , MONTH , DAY , HOUR , MINUTE , or SECOND . The EXTRACT( ) function is available as of MySQL 3.23.0.
5.6.4 See AlsoThe functions discussed in this recipe provide single components of temporal values. If you want to produce a value consisting of multiple components from a given value, it may be more convenient to use DATE_FORMAT( ) . See Recipe 5.5. |
|
MySQL Cookbook Authors: DuBois P. Published year: 2005 Pages: 112/412 |
![]() Learning MySQL | ![]() MySQL Stored Procedure Programming | ![]() PHP Cookbook: Solutions and Examples for PHP Programmers | ![]() MYSQL in a Nutshell (In a Nutshell (O'Reilly)) | ![]() MySQL (4th Edition) |
![]() Learning MySQL | ![]() MySQL Stored Procedure Programming |
![]() PHP Cookbook: Solutions and Examples for PHP Programmers | ![]() MYSQL in a Nutshell (In a Nutshell (O'Reilly)) |
![]() MySQL (4th Edition) |