THE WORLD'S LARGEST WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

MySQL EXTRACT() Function


SQL Dates MySQL Date Functions

Definition and Usage

The EXTRACT() function is used to return a single part of a date/time, such as year, month, day, hour, minute, etc.

Syntax

EXTRACT(unit FROM date)

Where date is a valid date expression and unit can be one of the following:

Unit Value
MICROSECOND
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUARTER
YEAR
SECOND_MICROSECOND
MINUTE_MICROSECOND
MINUTE_SECOND
HOUR_MICROSECOND
HOUR_SECOND
HOUR_MINUTE
DAY_MICROSECOND
DAY_SECOND
DAY_MINUTE
DAY_HOUR
YEAR_MONTH

Example

Assume we have the following "Orders" table:

OrderId ProductName OrderDate
1 Jarlsberg Cheese 2014-11-22 13:23:44.657

The following SELECT statement:

SELECT EXTRACT(YEAR FROM OrderDate) AS OrderYear,
EXTRACT(MONTH FROM OrderDate) AS OrderMonth,
EXTRACT(DAY FROM OrderDate) AS OrderDay
FROM Orders
WHERE OrderId=1

will result in this:

OrderYearOrderMonthOrderDay
20141122

SQL Dates MySQL Date Functions