w3resource

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

Pictorial presentation of postgresql EXTRACT function

Example 1: PostgreSQL EXTRACT() function

Code:

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

Sample Output:

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

Example 2: PostgreSQL EXTRACT() function

Code:

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

Sample Output:

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

Previous: DATE_TRUNC function
Next: ISFINITE function