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

Arguments Description
date Required. The date/datetime to extract the date from.
unit Required. 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');

4. mysql> SELECT EXTRACT(YEAR FROM OrderDate) AS OrderYear, EXTRACT(MONTH FROM OrderDate) AS OrderMonth   FROM t2 WHERE OrderId=1;
5. +-----------+------------+
6. | orderyear | ordermonth |
7. +-----------+------------+
8. | 2008      | 11         |
9. +-----------+------------+
10. 1 row in set (0.01 sec)

Constraints

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