Overview

The EXTRACT() function takes out a given part (field) from a specified source.

Syntax

EXTRACT (field FROM source);

Parameters

  • field: string or identifier specifying the part of the date / time to extract
  • source: date / time value to extract from

The table below shows the supported input and return types for the EXTRACT() function:

Input Type: sourcefieldReturn Type
TIMESTAMPYEAR, MONTH, DAY, HOUR, MINUTE, SECONDBIGINT
TIMESTAMPTZYEAR, MONTH, DAY, HOUR, MINUTE, SECONDBIGINT
DATEYEAR, MONTH, DAYINTEGER

Examples

EXTRACT() with Timestamp - Year

The below example uses the EXTRACT() function to extract a given timestamp’s YEAR:

SELECT EXTRACT(YEAR FROM TIMESTAMP '2020-12-31 13:30:15');

The final output will be as follows:

+----------+
| extract  |
+----------+
| 2020     |
+----------+

EXTRACT() with Timestamp - Month

Here we will use the EXTRACT() function to extract a given timestamp’s MONTH:

SELECT EXTRACT(MONTH FROM TIMESTAMP '2016-10-31 13:30:15');

The final output will take the month’s part of a given timestamp:

+----------+
| extract  |
+----------+
| 10       |
+----------+