Overview
TheEXTRACT() function retrieves a specified part (field) from a given date/time or interval value.
It is commonly used to obtain components such as year, month, day, hour, etc., from timestamps or dates.
Syntax
Parameters
field: string or identifier specifying the part of the date / time to extractsource: date / time value from which to extract the specifed field
EXTRACT() function:
Input Type: source | Supported field values | Return Type |
|---|---|---|
TIMESTAMP | YEAR, MONTH, DAY, HOUR, MINUTE, SECOND | DOUBLE PRECISION |
TIMESTAMPTZ | YEAR, MONTH, DAY, HOUR, MINUTE, SECOND | DOUBLE PRECISION |
DATE | YEAR, MONTH, DAY | INTEGER |
The SECOND field returns a fractional value as DOUBLE PRECISION to include fractional seconds, not an integer type
Examples
EXTRACT() with Timestamp - Year
The below example uses theEXTRACT() function to extract a given timestamp’s YEAR:
EXTRACT() with Timestamp - Month
Here we will use theEXTRACT() function to extract a given timestamp’s MONTH:
EXTRACT() with Timestamp - Seconds (including fractional seconds)
Here we will use theEXTRACT() function to extract a given timestamp’s SECONDS: