Round date to 10 minutes interval

后端 未结 7 2040
独厮守ぢ
独厮守ぢ 2021-02-19 07:26

I have a DATE column that I want to round to the next-lower 10 minute interval in a query (see example below).

I managed to do it by truncating the seconds

7条回答
  •  孤城傲影
    2021-02-19 08:07

    I generally hate doing date -> character -> date conversions when it's not necessary. I'd rather use numbers.

    select trunc((sysdate - trunc(sysdate))*60*24,-1)/(60*24)+trunc(sysdate) from dual;     
    

    This extracts the minutes from the current day, truncates them down to the 10-minute interval, and then adds them back in to make it a date again. Of course, you can replace sysdate with whatever date you want. It trusts implicit conversions a lot more than I want but at least it'll work for any NLS date format.

提交回复
热议问题