The function DATE_TRUNC
is conceptually similar to the TRUNC
function for numbers. The return value is of type timestamp
or interval
with all fields that are less significant than the selected one set to zero (or one, for day and month).
Syntax
DATE_TRUNC('field', source)
Semantics
field is a string constant that selects the precision to which truncate the input value. Valid values for field are:
century day decade hour microseconds millennium |
milliseconds minute month second week year |
source is a value expression of type TIMESTAMP
or INTERVAL
. (Values of type DATE
and TIME
are cast automatically, to TIMESTAMP
or INTERVAL
respectively.)
Examples
SELECT DATE_TRUNC('hour', TIMESTAMP '2001-02-16 20:38:40');
Result: 2001-02-16 20:00:00
SELECT DATE_TRUNC('year', TIMESTAMP '2001-02-16 20:38:40');
Result: 2001-01-01 00:00:00