Skip to main content

EXTRACT

Categories: Date/Time

EXTRACT

Extracts the specified date or time part from the date or timestamp.

Syntax

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

  • time_unit: The time unit that needs to be extracted from the date, time or timestamp. This must YEAR, MONTH, DAY, HOUR, MINUTE, or SECOND.
  • date_time_expression: The date, time or timestamp to extract time part from.

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