Thursday, February 13, 2020

date_part #8 of #100PostgreSQLFunctions

This post is a part of the project #100PostgreSQLFunctions, which aims to explain the meaning of 100 PostgreSQL functions and how to use them. For the full list of functions click here.

date_part is a function to extract subfields of a value, which has to be one of the following datatypes: date, timestamp or interval.
This function has two argument:
  • subfield is the part of the value we want to extract
  • source represents the value from which we want to extract the subfield

Examples:

SELECT
    date_part('month', '2/20/2019'::date);

 date_part
-----------
         2
(1 row)


SELECT
    date_part('second', '2/20/2019 12:5:34'::timestamp);

 date_part
-----------
        34
(1 row)


SELECT
    date_part('day', '6 years 5 months 4 days'::interval);

 date_part
-----------
         4
(1 row)

No comments:

Post a Comment