--固定层数 SELECT * FROM DUAL CONNECT BY LEVEL < 10 --固定层数 SELECT LEVEL,SYSDATE+LEVEL FROM DUAL CONNECT BY LEVEL < (SYSDATE+10)-(SYSDATE);
结果图
有些人喜欢用rownum代替level。我不推荐使用rownum
请测试下面结果:
--获取日期时间段 SELECT TO_CHAR(TO_DATE('20190825','YYYYMMDD')+LEVEL-1,'YYYYMMDD') AS 日期时间段 FROM DUAL CONNECT BY LEVEL <= TO_DATE('20190901','YYYYMMDD')-TO_DATE('20190825','YYYYMMDD') --获取月份时间段 SELECT TO_CHAR(ADD_MONTHS(TO_DATE('20180901','YYYYMMDD'),LEVEL-1),'YYYYMM') AS 月份时间段 FROM DUAL CONNECT BY LEVEL<= MONTHS_BETWEEN(TO_DATE('20190901','YYYYMMDD'),TO_DATE('20180901','YYYYMMDD')); --获取年份时间段 SELECT TO_CHAR(ADD_MONTHS(TO_DATE('20150901','YYYYMMDD'),(LEVEL-1)*12),'YYYY') AS 年份时间段 FROM DUAL CONNECT BY LEVEL <= MONTHS_BETWEEN(TO_DATE('20190901','YYYYMMDD'),TO_DATE('20150901','YYYYMMDD'))/12;