MySQL EXTRACT() Function

In MySQL, the EXTRACT() function extracts and returns the specified part from the specified date/time.

EXTRACT() Syntax

Here is the syntax of MySQL EXTRACT() function:

EXTRACT(unit FROM date)

Parameters

unit
Required. The ID of the part that needs to be extracted.
date
Required. A date or datetime expression.

unit 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

Return value

MySQL EXTRACT() Function Function to extract the specified part from the specified date/time and return it.

If the specified expression is not a valid date or datetime, the EXTRACT() function will return NULL.

If any argument is NULL, the EXTRACT() function will return NULL.

EXTRACT() Examples

Here are some examples of the EXTRACT() function.

SELECT
    EXTRACT(YEAR FROM '2022-02-28'),
    EXTRACT(MONTH FROM '2022-02-28'),
    EXTRACT(DAY FROM '2022-02-28'),
    EXTRACT(HOUR FROM '10:11:12'),
    EXTRACT(MINUTE FROM '10:11:12'),
    EXTRACT(SECOND FROM '10:11:12'),
    EXTRACT(HOUR FROM '2022-02-28 10:11:12'),
    EXTRACT(MINUTE FROM '2022-02-28 10:11:12'),
    EXTRACT(SECOND FROM '2022-02-28 10:11:12')\G
           EXTRACT(YEAR FROM '2022-02-28'): 2022
          EXTRACT(MONTH FROM '2022-02-28'): 2
            EXTRACT(DAY FROM '2022-02-28'): 28
             EXTRACT(HOUR FROM '10:11:12'): 10
           EXTRACT(MINUTE FROM '10:11:12'): 11
           EXTRACT(SECOND FROM '10:11:12'): 12
  EXTRACT(HOUR FROM '2022-02-28 10:11:12'): 10
EXTRACT(MINUTE FROM '2022-02-28 10:11:12'): 11
EXTRACT(SECOND FROM '2022-02-28 10:11:12'): 12