简体   繁体   中英

Oracle SQL: TO_DATE Logic

I am currently getting an ORA-01841 from my query and I think it may be due to the below logic within the query (Something I did not implement). What is the below logic attempting to do?

AND (EVENT_DAY_LOCAL BETWEEN ADD_MONTHS(TRUNC(TO_DATE('{2015/11/14}','YYYY/MM/DD'),'D'),-2)
                AND TO_DATE('{2015/11/15}','YYYY/MM/DD')
            OR EVENT_DAY_LOCAL BETWEEN ADD_MONTHS(TRUNC(TO_DATE('{2015/11/14}','YYYY/MM/DD'),'D'),-13)
                AND (ADD_MONTHS(TRUNC(TO_DATE('{2015/11/15}','YYYY/MM/DD'),'D'),-12)-14)

The brackets around the date are causing the ORA-01841 error so remove them as shown below and it should run. As for what they query is doing, it is determining the week start date and only returning records where EVENT_DAY_LOCAL is from the last two months or is between Oct 8th and Nov 1st 2014.

SQL fiddle of the logic

--removing the brackets should fix the error
AND (EVENT_DAY_LOCAL BETWEEN ADD_MONTHS(TRUNC(TO_DATE('2015/11/14','YYYY/MM/DD'),'D'),-2)
                AND TO_DATE('2015/11/15','YYYY/MM/DD')
            OR EVENT_DAY_LOCAL BETWEEN ADD_MONTHS(TRUNC(TO_DATE('2015/11/14','YYYY/MM/DD'),'D'),-13)
                AND (ADD_MONTHS(TRUNC(TO_DATE('2015/11/15','YYYY/MM/DD'),'D'),-12)-14)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM