Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative of trunc function in postgresql

Tags:

sql

postgresql

I am using trunc function in postgresql like trunc(date) , but error is there "Function trunc(date) does not exist in postgresql". Please tell me the alternative function of trunc in postgresql

like image 225
Dolly Kumbhalkar Avatar asked Sep 06 '25 03:09

Dolly Kumbhalkar


2 Answers

I think you are looking for the date_trunc() function, which is used to truncate timestamps. The trunc() function is used for truncating numbers, not dates.

If you had a date and you wanted to truncate it to the hour, you could use:

date_trunc('hour', date)

If you wanted to truncate to the day, you could use this:

date_trunc('day', date)

Documentation

like image 52
Tim Biegeleisen Avatar answered Sep 07 '25 21:09

Tim Biegeleisen


if you only want cut to hour you can cast timestamp variable to date example

timestamp_variable::date
like image 35
Piotr Rogowski Avatar answered Sep 07 '25 20:09

Piotr Rogowski