PostgreSQL EXTRACT Function

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PostgreSQL EXTRACT() function

EXTRACT() function

The date_trunc() function is used to get subfield.

The extract function() is used to retrieves subfields such as year or hour from date/time values. The source must be a value expression of type timestamp, time, or interval. The field is an identifier or string that selects what field to be extracted from the source value.

Syntax :

extract(field from timestamp)
or
extract(field from interval)

Return Type : double precision.

PostgreSQL Version : 9.3

Pictorial Presentation of PostgreSQL EXTRACT() function

 

Example 1: PostgreSQL EXTRACT() function

SELECT extract(hour from timestamp '2002-09-17 19:27:45');

Output :

 date_part
-----------
        19
(1 row)

Example 2: PostgreSQL EXTRACT() function

SELECT extract(month from interval '3 years 7 months');

Output :

 date_part
-----------
         7
(1 row)