EXTRACT
Overview
The EXTRACT()
function extracts a given part from a specified source. The source must be a value expression of timestamp, time, or interval.
Syntax
Let’s analyze the above syntax:
-
field
: used to specify the time parts that are to be extracted. -
source
: used to identify a date/time value. The value type isTIMESTAMP
(YYYY-MM-DD HH:MM:SS) orINTERVAL
(year - month - day hour - minute - second).
Input and Return Type
The table below shows the supported input types of field
and the return type of the EXTRACT()
function:
Examples
#Case 1: EXTRACT()
with Timestamp - Year
The below example uses the EXTRACT()
function to extract a given timestamp’s YEAR:
The final output will be as follows:
#Case 2: EXTRACT()
with Timestamp - Month
Here we will use the EXTRACT()
function to extract a given timestamp’s MONTH:
The final output will take the month’s part of a given timestamp:
#Case 3: EXTRACT()
with Interval - Day
In this example, we will extract the **DAY **part of a given interval:
The final output will be as follows:
#Case 4: EXTRACT()
with Interval - Second
The below example uses the EXTRACT()
function to extract seconds from the given interval.
The final output will be as follows: