Thursday, February 13, 2020

date_trunc #7 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_trunc is a function to truncate a timestamp value, or an interval value, with a level of precision.
This function has two argument:
  • datapart is the level of precision
  • field is the timestamp value or the interval value to truncate

Examples:

SELECT
    date_trunc('month', '1/30/2020'::timestamp);

     date_trunc   
---------------------
 2020-01-01 00:00:00
(1 row)


SELECT
    date_trunc('month', '6 years 3 months 30 days'::interval);

   date_trunc 
----------------
 6 years 3 mons
(1 row)

No comments:

Post a Comment