w3resource

PostgreSQL DATE_PART() function

DATE_PART() function

The PostgreSQL DATE_PART() function is used to extract a specific part of a date and time value, such as the year, month, day, hour, minute, second, etc.

Uses of DATE_PART() Function
  • Extract Year: Retrieve the year part from a timestamp or date.

  • Extract Month: Retrieve the month part from a timestamp, date, or interval.

  • Extract Day: Retrieve the day part from a timestamp or date.

  • Extract Hour: Retrieve the hour part from a timestamp.

  • Extract Minute: Retrieve the minute part from a timestamp.

  • Extract Second: Retrieve the second part from a timestamp.

  • Extract Part from Interval: Retrieve specific parts from an interval, such as years, months, days, etc.

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



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/PostgreSQL/date_part-function.php