Skip to main content

Categories: Date/Time

EXTRACT

Extracts the specified time unit from the specified DATE, TIME, or TIMESTAMP. The WEEK part uses ISO 8601 week numbering. Returns NULL if the input is NULL.

Syntax​

EXTRACT(time_unit KEYWORD, date_time_expression DATE, TIME, TIMESTAMP) → NUMERIC​

  • time_unit: The time unit to extract from the date, time, or timestamp. Valid values are YEAR, QUARTER, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND, DOW, DOY, DECADE, CENTURY, MILLENNIUM, or EPOCH.
  • date_time_expression: The date, time, or timestamp from which to extract the specified time unit.

Examples

EXTRACT example
SELECT EXTRACT(HOUR FROM CAST('05:33:44' AS TIME))
-- 5
EXTRACT example
SELECT EXTRACT(MONTH FROM CAST('2021-03-22 05:33:44.2' AS TIMESTAMP))
-- 3
EXTRACT example
SELECT EXTRACT(SECOND FROM CAST('2021-03-22 05:33:44.2' AS TIMESTAMP))
-- 44
EXTRACT example
SELECT EXTRACT(YEAR FROM CAST('2021-03-22' AS DATE))
-- 2021
EXTRACT example
SELECT EXTRACT(EPOCH FROM CAST('2021-03-22 05:33:44.2' AS TIMESTAMP))
-- 1616391224
EXTRACT example
SELECT EXTRACT(EPOCH FROM CAST('2021-03-22' AS DATE))
-- 1616371200
Extract the day from a date
SELECT EXTRACT(DAY FROM DATE '2026-03-18')
-- 18
Extract the minute from a timestamp
SELECT EXTRACT(MINUTE FROM TIMESTAMP '2026-03-18 14:30:45')
-- 30
Extract the day of the week from a date
SELECT EXTRACT(DOW FROM DATE '2026-03-18')
-- 4
Extract the day of the year from a date
SELECT EXTRACT(DOY FROM DATE '2026-03-18')
-- 77
Extract the ISO week number from a date
SELECT EXTRACT(WEEK FROM DATE '2026-03-18')
-- 12
Extract the quarter from a date
SELECT EXTRACT(QUARTER FROM DATE '2026-03-18')
-- 1
Extract the decade from a date
SELECT EXTRACT(DECADE FROM DATE '2026-03-18')
-- 202
Extract the century from a date
SELECT EXTRACT(CENTURY FROM DATE '2026-03-18')
-- 21
Extract the millennium from a date
SELECT EXTRACT(MILLENNIUM FROM DATE '2026-03-18')
-- 3
Return NULL for a NULL input
SELECT EXTRACT(YEAR FROM CAST(NULL AS DATE))
-- null