Oracle获取每分钟、每10分钟、每小时、每天、每周、每月、每年的时间或日期

-- 获取某天的每分钟时间
SELECT TO_DATE('2018-03-23', 'YYYY-MM-DD') + (ROWNUM - 1) / (24 * 60)
FROM DUAL CONNECT BY ROWNUM <= 24*60;

-- 获取某天的每10分钟时间
SELECT TO_DATE('2018-03-23', 'YYYY-MM-DD') + (ROWNUM - 1) / (24 * 6)
FROM DUAL CONNECT BY ROWNUM <= 24*6;

-- 获取某天每小时的时间
SELECT TO_DATE('2018-03-23', 'YYYY-MM-DD') + (ROWNUM - 1) / 24
FROM DUAL CONNECT BY ROWNUM <= 24;

-- 获取某时间段内每天的日期
SELECT TO_DATE('2018-03-23', 'YYYY-MM-DD') + (ROWNUM - 1)
FROM DUAL CONNECT BY ROWNUM <=
TRUNC(TO_DATE('2020-03-23', 'YYYY-MM-DD')) - TRUNC(TO_DATE('2018-03-23', 'YYYY-MM-DD'));

-- 获取某时间段内每周的起始日期和其周数
SELECT TRUNC(TO_DATE('2018-03-02', 'YYYY-MM-DD') + (ROWNUM - 1) * 7, 'D') + 1 AS STARTTIME,
TO_CHAR(TO_DATE('2018-03-02', 'YYYY-MM-DD') + (ROWNUM - 1) * 7, 'IW') AS WEEK
FROM DUAL CONNECT BY ROWNUM <=
TRUNC(TO_DATE('2019-03-02', 'YYYY-MM-DD') - TO_DATE('2018-03-02', 'YYYY-MM-DD')) / 7 + 1;

-- 获取某时间段内的每月
SELECT TO_CHAR(ADD_MONTHS(TO_DATE('2018-03-02', 'YYYY-MM-DD'), ROWNUM - 1), 'YYYY-MM')
FROM DUAL CONNECT BY ROWNUM <=
CEIL(MONTHS_BETWEEN(TO_DATE('2020-03-03', 'YYYY-MM-DD'), TO_DATE('2018-03-02','YYYY-MM-DD')));

-- 获取某时间段内每年的年份
SELECT TO_CHAR(ADD_MONTHS(TO_DATE('2018-03-02', 'YYYY-MM-DD'), (ROWNUM - 1) * 12), 'YYYY')
FROM DUAL CONNECT BY ROWNUM <=
CEIL(MONTHS_BETWEEN(TO_DATE('2020-03-03', 'YYYY-MM-DD'), TO_DATE('2018-03-02','YYYY-MM-DD')) / 12);

posted @ 2022-05-03 17:37  yclizq  阅读(1232)  评论(0编辑  收藏  举报