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 exampleSELECT EXTRACT(HOUR FROM CAST('05:33:44' AS TIME))
-- 5
SELECT EXTRACT(MONTH FROM CAST('2021-03-22 05:33:44.2' AS TIMESTAMP))
-- 3
SELECT EXTRACT(SECOND FROM CAST('2021-03-22 05:33:44.2' AS TIMESTAMP))
-- 44
SELECT EXTRACT(YEAR FROM CAST('2021-03-22' AS DATE))
-- 2021
SELECT EXTRACT(EPOCH FROM CAST('2021-03-22 05:33:44.2' AS TIMESTAMP))
-- 1616391224
SELECT EXTRACT(EPOCH FROM CAST('2021-03-22' AS DATE))
-- 1616371200