Jump to: navigation, search

PostgreSQL DATE TRUNC

From w3cyberlearnings

Contents

Postgresql date_trunc

This function returns the field value from the expression (date, timestamp, or interval)

Syntax date_trunc

  • field: sets the field name
  • expre: date, or datetime expression
date_trunc(field, expre);

Field

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

Example 1

postgres=# SELECT date_trunc('day',timestamp '2015-03-10 10:50:30');
     date_trunc      
---------------------
 2015-03-10 00:00:00
(1 row)

postgres=# SELECT date_trunc('minute',timestamp '2015-03-10 10:50:30');
     date_trunc      
---------------------
 2015-03-10 10:50:00
(1 row)

Example 2

postgres=# SELECT name, age, date_trunc('month',age)
FROM employee;
  name  |    age     |       date_trunc       
--------+------------+------------------------
 John   | 1990-04-13 | 1990-04-01 00:00:00-06
 Week   | 1991-06-12 | 1991-06-01 00:00:00-05
 Christ | 1995-05-17 | 1995-05-01 00:00:00-05
(3 rows)

Related Links



Navigation
Web
SQL
MISC
References