EXTRACT
The EXTRACT function in SQL is used to retrieve a specific datetime field from a date, time, or interval.
Syntax
EXTRACT(field FROM source)
field: The datetime field to extract. Valid fields includeYEAR,MONTH,DAY,HOUR,MINUTE,SECOND.source: The date, time, or interval value from which the datetime field is to be extracted.
Usage
Here are examples of how EXTRACT can be used to pull specific datetime components from various types of datetime and interval data.
-
Extracting the
HOURfrom aTIMESTAMP:SELECT EXTRACT(HOUR FROM TIMESTAMP '2016-12-31 13:30:15') as extract;This returns
13. -
Extracting the
YEARfrom aTIMESTAMP:SELECT EXTRACT(YEAR FROM TIMESTAMP '2016-12-31 13:30:15') as extract;This returns
2016. -
Extracting the
SECONDfrom aTIMEvalue:SELECT EXTRACT(SECOND FROM TIME '17:12:28') as extract;This returns
28. -
Extracting the
DAYfrom aDATEvalue:SELECT EXTRACT(DAY FROM DATE '2021-10-06') as extract;This returns
6. -
Extracting from
INTERVALdata:SELECT EXTRACT(YEAR FROM INTERVAL '3' YEAR) as extract;
SELECT EXTRACT(MINUTE FROM INTERVAL '7' MINUTE) as extract;These return
3and7, respectively.
Note that the EXTRACT function expects the source to be of a compatible datetime or interval type. Using a value of an incompatible type, such as a number or a string that cannot be interpreted as a datetime, will result in an error.