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

Visual Presentation of PostgreSQL DATE_TRUNC() function

Pictorial presentation of postgresql DATE_TRUNC function


Example: PostgreSQL DATE_TRUNC() function :

Example:

SQL Code:

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

Output:

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

Previous: DATE_PART function
Next: EXTRACT function



Follow us on Facebook and Twitter for latest update.