Name

EXTRACT()

Synopsis

EXTRACT(type FROM expression)

This function extracts date or time information from a date or a datetime expression in the format type requested. The acceptable types are the same as the intervals for DATE_ADD(). See Table 12-1 earlier in this chapter under that function for a list of intervals permitted. Here is an example:

SELECT NOW( ) AS 'Time Now', 
EXTRACT(HOUR_MINUTE FROM NOW( )) AS "Now in 'hhmm' format";

+---------------------+----------------------+
| Time Now            | Now in 'hhmm' format |
+---------------------+----------------------+
| 2008-03-14 20:36:04 |                 2036 | 
+---------------------+----------------------+

Get MySQL in a Nutshell, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.