Round date to 10 minutes interval

后端 未结 7 2070
独厮守ぢ
独厮守ぢ 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:14

    To return the next upper 10 minute interval, I used the following query. I hope it'll be useful because I couldn't simply do a

    trunc(sysdate, 'mi') + mod(EXTRACT(minute FROM cast(sysdate as timestamp)), 10) / (24 * 60)

    I did this and it worked for me.

    select 
    case when mod(EXTRACT(minute FROM cast(sysdate as timestamp)),5) between 1 and 4
          then trunc(sysdate,'mi')+((5*TRUNC(EXTRACT(minute FROM cast(sysdate as timestamp))/5,
    0)+5)-EXTRACT(minute FROM cast(sysdate as timestamp)))/1440
          else trunc(sysdate,'mi')
    end
    from dual

    This is based on this post.

提交回复
热议问题