postgresqlHow do I use the Postgresql date_trunc function?
The Postgresql date_trunc
function is used to truncate a timestamp to a specified precision. It takes two arguments, a text string specifying the precision, and a timestamp.
For example, to truncate a timestamp to the nearest hour, you can use the following code:
SELECT date_trunc('hour', timestamp '2020-06-12 10:30:45');
The output of this query would be 2020-06-12 10:00:00
.
The precision argument can be one of the following values:
millennium
century
decade
year
quarter
month
week
day
hour
minute
second
For example, to truncate a timestamp to the nearest day, you can use the following code:
SELECT date_trunc('day', timestamp '2020-06-12 10:30:45');
The output of this query would be 2020-06-12 00:00:00
.
Helpful links
More of Postgresql
- How can Zalando use PostgreSQL to improve its software development?
- How can I troubleshoot zero damaged pages in PostgreSQL?
- How can I use PostgreSQL and ZFS snapshots together?
- How do I use PostgreSQL's XMLTABLE to parse XML data?
- How can I set a PostgreSQL interval to zero?
- How do I use PostgreSQL ZonedDateTime to store date and time information?
- How do I parse XML data using PostgreSQL?
- How can I use PostgreSQL with YAML?
- How do I show tables in PostgreSQL?
- How do I rename a table in PostgreSQL?
See more codes...