Oracle 计算两个日期之间的月份数_月份列表_天数_日期列表
获取日期之间的月数(包括自身)
第一种:
select (MONTHS_BETWEEN ( TRUNC(TO_DATE('2018-08-01','YYYY-MM-DD'),'MONTH'), TRUNC(TO_DATE('2018-05-30','YYYY-MM-DD'),'MONTH') ) + 1) MONTH_COUNT from dual;
第二种:
select (MONTHS_BETWEEN ( to_date(to_char(sysdate,'yyyy-MM'),'yyyy-MM'), to_date(to_char(to_date('2019-06-30','yyyy-MM-dd'),'yyyy-MM'),'yyyy-MM') ) + 1) MONTH_COUNT from dual;
获取日期之间的月份列表(包括自身)
select TO_CHAR(ADD_MONTHS(TO_DATE('2019-08-01','YYYY-MM-DD'),(ROWNUM - 1)), 'yyyy-mm') MOHTN_ID from dual CONNECT BY ROWNUM <= MONTHS_BETWEEN ( TRUNC(TO_DATE('2020-01-01','YYYY-MM-DD'),'MONTH'), TRUNC(TO_DATE('2019-08-01','YYYY-MM-DD'),'MONTH') ) + 1;
结果: MOHTN_ID 2019-08 2019-09 2019-10 2019-11 2019-12 2020-01
获取日期之间的天数、日期列表(包含自身)
SELECT TO_CHAR(TO_DATE('2018-01-28', 'YYYY-MM-DD') + ROWNUM - 1, 'YYYY-MM-DD') DAY_ID FROM DUAL CONNECT BY ROWNUM <= TO_DATE('2018-02-04', 'YYYY-MM-DD') - TO_DATE('2018-01-28', 'YYYY-MM-DD') + 1;
结果: DAY_ID 2018-01-28 2018-01-29 2018-01-30 2018-01-31 2018-02-01 2018-02-02 2018-02-03 2018-02-04