ORACLE 根據時間段取的所有 月---周
阿新 • • 發佈:2018-12-14
獲取日期列表:
SELECT TO_CHAR(TO_DATE('2014-10-01', 'yyyy-MM-dd') + ROWNUM - 1, 'yyyyMMdd') as daylist
FROM DUAL
CONNECT BY ROWNUM <=
trunc(to_date('2015-06-01', 'yyyy-MM-dd') -
to_date('2014-10-01', 'yyyy-MM-dd')) + 1
獲取月份列表:
SELECT TO_CHAR(ADD_MONTHS(TO_DATE('2014-10', 'yyyy-MM'), ROWNUM - 1), 'yyyyMM') as monthlist FROM DUAL CONNECT BY ROWNUM <= months_between(to_date('2015-06', 'yyyy-MM'), to_date('2014-10', 'yyyy-MM')) + 1
獲取周列表: 獲取自然周 以週一開始 - 週日結束
SELECT trunc(to_DATE('2018-09-02', 'YYYY-MM-DD') + (ROWNUM - 1) * 7, 'iw') AS mon, trunc(to_DATE('2018-09-02', 'YYYY-MM-DD') + (ROWNUM - 1) * 7, 'iw') + 6 AS sun FROM DUAL CONNECT BY ROWNUM <= (trunc(to_DATE('2018-10-31', 'YYYY-MM-DD'), 'iw') + 6 - trunc(to_DATE('2018-09-02', 'YYYY-MM-DD'), 'iw')) / 7 + 1
獲取年份列表:
SELECT TO_CHAR(ADD_MONTHS(TO_DATE('2014-10', 'yyyy-MM'), (ROWNUM - 1) * 12),
'yyyy') as yearlist
FROM DUAL
CONNECT BY ROWNUM <=
months_between(to_date('2015-06', 'yyyy-MM'),
to_date('2014-10', 'yyyy-MM')) / 12 + 1