The Oracle database queries the year, month, day, and 24 hours (Instance) of the day in the time range according to the time range, and the oracle database
Year in the query time range
SELECT TO_CHAR ( ADD_MONTHS ( TO_DATE ('2013', 'yyyy'), (ROWNUM - 1) * 12 ), 'yyyy' ) AS yearsFROM DUAL CONNECT BY ROWNUM <= MONTHS_BETWEEN ( TO_DATE ('2018', 'yyyy'), TO_DATE ('2013', 'yyyy') ) / 12 + 1
Query the corresponding month in the time range
SELECT TO_CHAR ( ADD_MONTHS ( TO_DATE ('2017-10', 'yyyy-MM'), ROWNUM - 1 ), 'yyyy-MM' ) AS monthsFROM DUAL CONNECT BY ROWNUM <= MONTHS_BETWEEN ( TO_DATE ('2018-04', 'yyyy-MM'), TO_DATE ('2017-10', 'yyyy-MM') ) + 1
Query the date corresponding to the time range
SELECT TO_CHAR ( TO_DATE ('2017-11-01', 'yyyy-MM-dd') + ROWNUM - 1, 'yyyy-MM-dd' ) AS TIMEFROM DUAL CONNECT BY ROWNUM <= TRUNC ( TO_DATE ('2017-12-01', 'yyyy-MM-dd') - TO_DATE ('2017-11-01', 'yyyy-MM-dd') ) + 1
Query the 24 hours of a day
SELECT TO_CHAR ( TO_DATE ('2017-01-01', 'yyyy-mm-dd') + (ROWNUM - 1) / 24, 'hh24' ) AS HOURFROM dual CONNECT BY 24 >= ROWNUM
Of course, the preceding SQL statements are based on the Oracle database and are not suitable for other databases.
Query the year, month, and day in a certain period of time, mainly used to left join other data. Generally speaking, it is more practical to query report data. Next I will write the SQL statements about the 24-hour SQL statement of the SQL server database query time range, namely year, month, day, and day.