Skip to main content

Categories: Date/Time

EXTRACT

Extracts the specified time unit from the specified date, time, or timestamp.

Syntax

EXTRACT(time_unit KEYWORD, date_time_expression DATE, TIME, TIMESTAMP) → INTEGER

  • time_unit: The time unit to extract from the date, time, or timestamp. Valid values are EPOCH, YEAR, MONTH, DAY, HOUR, MINUTE, or SECOND.
  • 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