w3resource

PostgreSQL DATE_TRUNC() function

DATE_TRUNC() function

The PostgreSQL DATE_TRUNC() function is used to truncate a timestamp to the specified precision, such as hour, day, week, month, etc.

Uses of DATE_TRUNC() Function
  • Truncate to Year: Truncate a timestamp to the beginning of the year.

  • Truncate to Month: Truncate a timestamp to the beginning of the month.

  • Truncate to Day: Truncate a timestamp to the beginning of the day.

  • Truncate to Hour: Truncate a timestamp to the beginning of the hour.

  • Truncate to Minute: Truncate a timestamp to the beginning of the minute.

  • Truncate to Second: Truncate a timestamp to the beginning of the second.

Syntax:

date_trunc(text, timestamp)

Return Type: timestamp.

PostgreSQL Version: 9.3

Pictorial Presentation of PostgreSQL DATE_TRUNC() function

Pictorial presentation of postgresql DATE_TRUNC function

Example: PostgreSQL DATE_TRUNC() function :

Example:

Code:

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

Sample Output:

     date_trunc
---------------------
 2002-09-17 19:00:00
(1 row)

Previous: DATE_PART function
Next: EXTRACT 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://w3resource.com/PostgreSQL/date_trunc-function.php