Round date to 10 minutes interval

后端 未结 7 1966
独厮守ぢ
独厮守ぢ 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.

    0 讨论(0)
  • 2021-02-19 08:11

    Not necessarily any better, but another method:

    WITH test_data AS (
            SELECT TO_DATE('2010-01-01 10:00:00', 'YYYY-MM-DD HH24:MI:SS') d FROM dual
      UNION SELECT TO_DATE('2010-01-01 10:05:00', 'YYYY-MM-DD HH24:MI:SS') d FROM dual
      UNION SELECT TO_DATE('2010-01-01 10:09:59', 'YYYY-MM-DD HH24:MI:SS') d FROM dual
      UNION SELECT TO_DATE('2010-01-01 10:10:00', 'YYYY-MM-DD HH24:MI:SS') d FROM dual
      UNION SELECT TO_DATE('2099-01-01 10:00:33', 'YYYY-MM-DD HH24:MI:SS') d FROM dual
    )
    -- #end of test-data
    SELECT
      d, TRUNC(d) + FLOOR((d-TRUNC(d))*24*6)/(24*6)
    FROM test_data
    
    0 讨论(0)
  • 2021-02-19 08:12

    You could take the returned value as a string and substring the left side up to the last minute digit and replace it with a 0. I wouldn't exactly say thats better unless you provide some kind of metric.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-19 08:15

    I think to solve this there's a much easier and faster way to round to a next lower 10 seconds, 1 Minute, 10 Minute etc. interval. Try to manipulate your timestamp as a string using SUBSTR() like this:

    SELECT 
    SUBSTR(datetime(d),1,18)||'0' AS Slot10sec, 
    SUBSTR(datetime(d),1,17)||'00' AS Slot1min, 
    SUBSTR(datetime(d),1,15)||'0:00' AS Slot10min, 
    SUBSTR(datetime(d),1,14)||'00:00' AS Slot1h, 
    MY_VALUE
    FROM MY_TABLE;
    
    0 讨论(0)
  • 2021-02-19 08:24
    select
      trunc(sysdate, 'mi')
      - numtodsinterval(mod(EXTRACT(minute FROM cast(sysdate as timestamp)), 10), 'minute')
    from dual;
    

    or even

    select
      trunc(sysdate, 'mi')
      - mod(EXTRACT(minute FROM cast(sysdate as timestamp)), 10) / (24 * 60)
    from dual;
    
    0 讨论(0)
提交回复
热议问题