DATE_TRUNC() Function in PostgreSQL

In Section we will learn how to use the PostgreSQL DATE_TRUNC() function. DATE_TRUNC() function is used to truncate a date or timestamp to a specified level of precision. It allows you to round down a date or timestamp to a specific unit, such as year, quarter, month, week, day, hour, etc

 

Syntax for DATE_TRUNC() Function in Postresql:

DATE_TRUNC(unit, source)

unit: Specifies the unit to which you want to truncate the date or timestamp (e.g., ‘year’, ‘quarter’, ‘month’, ‘week’, ‘day’, ‘hour’, etc.).

source: The date or timestamp that you want to truncate.

 

The values of the unit must be one of the following values:

  • century
  • decade
  • year
  • month
  • day
  • week
  • hour
  • minute
  • second
  • microseconds
  • milliseconds

 

 

PostgreSQL DATE_TRUNC() function example

Below are some of the Examples for DATE_TRUNC() function in postgresql.

 

1.  Truncate a timestamp to the beginning of the year: TRUNC() function in postgresql:

In the below Example we will be passing year  as “unit” argument and timestamp as “source” argument in order to truncate date or timestamp to the beginning of  year.

SELECT DATE_TRUNC('year', '2024-02-24 15:30:45'::timestamp) AS truncated_date;

This will truncate the timestamp to the beginning of the year 2024

Output:

DATE_TRUNC-Function-in-PostgreSQL-1

 

 

2. Truncate a timestamp to the beginning of the Month: TRUNC() function in postgresql:

In the below Example we will be passing month  as “unit” argument and timestamp as “source” argument in order to truncate date or timestamp to the beginning of  Month mentioned in the date.

SELECT DATE_TRUNC('month', '2024-02-24 15:30:45'::timestamp) AS truncated_date;

This will truncate the timestamp to the beginning of the Feb 2024

Output:

DATE_TRUNC-Function-in-PostgreSQL-2

 

 

3. Truncate a timestamp to the beginning of the week: TRUNC() function in postgresql:

In the below Example we will be passing week  as “unit” argument and timestamp as “source” argument in order to truncate date or timestamp to the beginning of week mentioned in the date.

SELECT DATE_TRUNC('week', '2024-02-24 15:30:45'::timestamp) AS truncated_date;

This will truncate the timestamp to the beginning of the week of date mentioned

Output:

DATE_TRUNC-Function-in-PostgreSQL-3

 

 

4. Truncate a timestamp to the nearest hour: TRUNC() function in postgresql:

In the below Example we will be passing hour as “unit” argument and timestamp as “source” argument in order to truncate date or timestamp to the nearest hour of the mentioned date.

SELECT DATE_TRUNC('hour', '2024-02-24 15:30:45'::timestamp) AS truncated_date;

This will truncate the timestamp to the nearest hour for the mentioned date

Output:

DATE_TRUNC-Function-in-PostgreSQL-4

 

 

5. Truncate a timestamp to the beginning of quarter: TRUNC() function in postgresql:

In the below Example we will be passing quarter as “unit” argument and timestamp as “source” argument in order to truncate date or timestamp to the beginning of quarter for the mentioned date.

SELECT DATE_TRUNC('quarter', '2024-02-24 15:30:45'::timestamp) AS truncated_date;

This will truncate the timestamp to the beginning of the quarter

Output:

DATE_TRUNC-Function-in-PostgreSQL-5

 

 

6. Truncate a timestamp to the beginning of Decade: TRUNC() function in postgresql:

In the below Example we will be passing decade as “unit” argument and timestamp as “source” argument in order to truncate date or timestamp to the beginning of decade for the mentioned date.

SELECT DATE_TRUNC('decade', '2024-02-24'::date) AS truncated_date;

This will truncate the timestamp to the beginning of the decade

Output:

DATE_TRUNC-Function-in-PostgreSQL-6

 

 

7. Truncate a timestamp to the beginning of day: TRUNC() function in postgresql:

In the below Example we will be passing day as “unit” argument and timestamp as “source” argument in order to truncate date or timestamp to the beginning of day for the mentioned date.

SELECT DATE_TRUNC('day', '2024-02-24'::date) AS truncated_date;

This will truncate the timestamp to the beginning of the day

Output:

DATE_TRUNC-Function-in-PostgreSQL-7

 

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.