THE WORLD'S LARGEST WEB DEVELOPER SITE

MySQL EXTRACT() Function

❮ MySQL Functions

Example

Extract the month from a date:

SELECT EXTRACT(MONTH FROM "2017-06-15");
Try it Yourself »

Definition and Usage

The EXTRACT() function extracts parts from a date.

Syntax

EXTRACT(unit FROM date)

Parameter Values

Parameter Description
unit Required. The unit type of the interval. Can be one of the following values:
  • 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
date Required. The date or datetime value from which the date part is to be extracted

Technical Details

Works in: MySQL 5.7, MySQL 5.6, MySQL 5.5, MySQL 5.1, MySQL 5.0, MySQL 4.1, MySQL 4.0, MySQL 3.23

More Examples

Example

Extract the week from a date:

SELECT EXTRACT(WEEK FROM "2017-06-15");
Try it Yourself »

Example

Extract the minute from a datetime:

SELECT EXTRACT(MINUTE FROM "2017-06-15 09:34:21");
Try it Yourself »

Example

Extract the year and month from a datetime:

SELECT EXTRACT(YEAR_MONTH FROM "2017-06-15 09:34:21");
Try it Yourself »

❮ MySQL Functions