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

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.