w3resource

PostgreSQL DATE_PART() function

DATE_PART() function

The date_part() function is used to return the part of a date and time.

Syntax:

date_part(text, timestamp)
or
date_part(text, interval)

Return Type: double precision.

PostgreSQL Version: 9.3

Pictorial Presentation of PostgreSQL DATE_PART() function

Pictorial presentation of postgresql DATE_PART function

Example 1: PostgreSQL DATE_PART() function

The example below finds the hour part from the timestamp (date and time specified in the argument) .

code:

SELECT date_part('hour', timestamp '2002-09-17 19:27:45');

Sample Output:

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

Example 2: PostgreSQL DATE_PART() function

The example below extracts the months part the given age.

Code:

SELECT date_part('month', interval '3 years 7 months');

Sample Output:

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

Previous: CURRENT_TIMESTAMP function
Next: DATE_TRUNC function



Share this Tutorial / Exercise on : Facebook and Twitter