标签:to_date 结束 to_char oracl 结束时间 rac list mon nec
-- 时间段内的每个月份,{0}:开始日期,{1}:结束日期 SELECT TO_CHAR(ADD_MONTHS(TO_DATE(‘{0}‘, ‘YYYY-MM‘), ROWNUM - 1), ‘YYYYMM‘) AS MONTHLIST FROM DUAL CONNECT BY ROWNUM <= MONTHS_BETWEEN(TO_DATE(‘{1}‘, ‘YYYY-MM‘), TO_DATE(‘{0}‘, ‘YYYY-MM‘)) + 1
例: SELECT TO_CHAR(ADD_MONTHS(TO_DATE(‘2017-01‘, ‘YYYY-MM‘), ROWNUM - 1), ‘YYYYMM‘) AS MONTHLIST FROM DUAL CONNECT BY ROWNUM <= MONTHS_BETWEEN(TO_DATE(‘2017-06‘, ‘YYYY-MM‘), TO_DATE(‘2017-01‘, ‘YYYY-MM‘)) + 1 执行结果: MONTHLIST 1 201701 2 201702 3 201703 4 201704 5 201705 6 201706
-- 时间段内的每周,{0}:开始时间,{1}:结束时间 SELECT TO_CHAR(TO_DATE(‘{0}‘, ‘YYYY-MM-DD‘) + (ROWNUM - 1) * 7, ‘IYYYIW‘) AS WEEKLIST FROM DUAL CONNECT BY ROWNUM <= (TRUNC(TO_DATE(‘{1}‘, ‘YYYY-MM-DD‘), ‘IW‘) - TRUNC(TO_DATE(‘{0}‘, ‘YYYY-MM-DD‘), ‘IW‘)) / 7 + 1 例: SELECT TO_CHAR(TO_DATE(‘2017-04-25‘, ‘YYYY-MM-DD‘) + (ROWNUM - 1) * 7, ‘IYYYIW‘) AS WEEKLIST FROM DUAL CONNECT BY ROWNUM <= (TRUNC(TO_DATE(‘2017-12-01‘, ‘YYYY-MM-DD‘), ‘IW‘) - TRUNC(TO_DATE(‘2017-04-25‘, ‘YYYY-MM-DD‘), ‘IW‘)) / 7 + 1 执行结果: WEEKLIST 1 201717 2 201718 3 201719 4 201720 5 201721 6 201722 7 201723 8 201724 9 201725 10 201726 11 201727 12 201728 13 201729 14 201730 15 201731 16 201732 17 201733 18 201734 19 201735 20 201736 21 201737 22 201738 23 201739 24 201740 25 201741 26 201742 27 201743 28 201744 29 201745 30 201746 31 201747 32 201748
记录完毕
标签:to_date 结束 to_char oracl 结束时间 rac list mon nec
原文地址:http://www.cnblogs.com/LLSutdy/p/7998322.html