Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Round datetime column to nearest hour in AWS redshift

I have a table called orders which contains a column full of date timestamps

order_date
2021-04-15 15:48:32
2021-04-14 13:02:54
...etc

and I want to round them to the nearest hour in a new column, I can't find a function which does this. DATE_TRUNC('hour', datetime) always rounds down rather than to the nearest hour. Ideally the outcome would look like:

order_date             rounded_date
2021-04-15 15:48:32    2021-04-21 16:00:00
2021-04-14 13:02:54    2021-04-14 13:00:00
like image 360
qazwsx123 Avatar asked Oct 31 '25 12:10

qazwsx123


1 Answers

The code is:

date_trunc('hour', order_date + interval '30 minute')
like image 199
Gordon Linoff Avatar answered Nov 03 '25 04:11

Gordon Linoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!