加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Oracle PLSQL将日期时间截断为15分钟

发布时间:2020-12-12 15:12:10 所属栏目:百科 来源:网络整理
导读:我想将我的数据汇总到15分钟段(一小时的季度).为此,我编写了一些生成15分钟日期时间块的代码. SELECT TRUNC(SYSDATE,'hh') + 0.25/24 - (ROWNUM) *0.25/ 24 AS time_start,ROWNUM,TRUNC(SYSDATE,'hh') + 0.25/24 - (ROWNUM - 1) *0.25/ 24 AS time_endFROM w
我想将我的数据汇总到15分钟段(一小时的季度).为此,我编写了一些生成15分钟日期时间块的代码.
SELECT 
   TRUNC(SYSDATE,'hh') + 0.25/24 - (ROWNUM) *0.25/ 24
   AS time_start,ROWNUM,TRUNC(SYSDATE,'hh') + 0.25/24 - (ROWNUM - 1) *0.25/ 24
   AS time_end
FROM widsys.consist 
WHERE ROWNUM <3000
ORDER BY sysdate

我的代码的问题是因为它使用了一个小时截断,它只会从最近一小时的开始生成时间戳.例如,现在是11:49 AM,因此生成的第一个标记是11:00 AM.

我需要它从最后15分钟块的开始(上面的例子11:45 AM)生成标记.谁能帮帮我吗?

这将为您提供最近的季度.
select sysdate,trunc(sysdate,'mi') -                           --truncate to the nearest minute
       numtodsinterval(                                --convert the minutes in number to interval type and subtract.
                       mod(to_char(sysdate,'mi'),15),--find the minutes from the nearest quarter
                      'minute'                          
                      ) as nearest_quarter
  from dual;

输出:

sysdate                             nearest_quarter
-----------------------------------------------------------------
October,11 2013 05:54:24+0000      October,11 2013 05:45:00+0000
October,11 2013 05:22:24+0000      October,11 2013 05:15:00+0000

使用它作为起始值,然后迭代它.

with cte as(
  select trunc(sysdate,'mi') - 
         numtodsinterval(mod(to_char(sysdate,'minute') as nearest_quarter
  from dual
  )
select nearest_quarter - numtodsinterval((level - 1)*15,'minute'),nearest_quarter - numtodsinterval((level - 2)*15,'minute')
from cte
connect by level <= 10;

Demo.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读