EXTRACT()

Description

The EXTRACT() function uses the same kinds of unit specifiers as DATE_ADD() or DATE_SUB(), but extracts parts from the date rather than performing date arithmetic. Returns NULL if date is NULL.

Syntax

  1. > EXTRACT(unit FROM date)

Arguments

ArgumentsDescription
dateRequired. The date/datetime to extract the date from.
unitRequired. The unit argument can have the following values:
MICROSECOND
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUA
TER
YEAR
SECOND_MICROSECOND
MINUTE_MICROSECOND
MINUTE_SECOND
HOUR_MICROSECOND
HOUR_SECOND
HOUR_MINUTE
DAY_MICROSECOND
DAY_SECOND
DAY_MINUTE
DAY_HOUR
YEAR_MONTH

Examples

  1. create table t2(orderid int, productname varchar(20), orderdate datetime);
  2. insert into t2 values ('1','Jarl','2008-11-11 13:23:44.657');
  3. mysql> SELECT EXTRACT(YEAR FROM OrderDate) AS OrderYear, EXTRACT(MONTH FROM OrderDate) AS OrderMonth FROM t2 WHERE OrderId=1;
  4. +-----------+------------+
  5. | orderyear | ordermonth |
  6. +-----------+------------+
  7. | 2008 | 11 |
  8. +-----------+------------+
  9. 1 row in set (0.01 sec)

Constraints

The date type supports only yyyy-mm-dd and yyyymmdd for now.