"Oracle" TRUNC: Get early and end of last year, get the month and month; __oracle

Source: Internet
Author: User
I. Demand

Current time: 2018-1-6 16:56:01
Because of the data in the database at the beginning of last year, that is (2017-1-1 00:00:00) and the end of last year (2017-12-31 23:59:00), the two time periods: ii. results

1, obtain early last year (2017-1-1 00:00:00) (>=) and the beginning of this year (2018-1-1 00:00:00) (can use less than <);


The SQL statement is as follows:

SELECT (TRUNC (sysdate, ' YYYY ')-interval ' 1 ' year), (TRUNC (sysdate, ' YYYY ')] from dual;

2. Access to the beginning of last year (2017-1-1-00:00:00) and the end of (2017-12-31-23:59:00)

Sql:

SELECT (TRUNC (sysdate, ' YYYY ')-interval ' 1 ' year), (TRUNC (sysdate, ' YYYY '))-interval ' 1 ' MINUTE from dual;

Referencing block contents

3. Get the month of last year (2017-1-1 00:00:00) (with >= number) and the end of (2017-2-1 00:00:00) (with <)

Sql:

SELECT (TRUNC (sysdate, ' mm ')-interval ' 1 '), Add_months ((TRUNC (sysdate, ' mm ')-interval ' 1 ' year), 1) from dual;

Third, Summary:
1, TRUNC (sysdate, ' format '): Format can write ' YYYY ', ' MM ', ' DD ' etc, to get different initial value;

2, can combine minus a "INTERVAL ' 1 ' Year", "INTERVAL ' 1 ' MONTH", "INTERVAL ' 1 ' Day" to precisely adjust;

3, you can use the Last_day () method to obtain the last day of the month, also can be obtained by the beginning of last month, minus 1 days;

4, Trunc seems to be accurate only to minutes, not to seconds, this is not sure, there is no big God knows how accurate to the second.

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.