Skip to Main Content

SQL Developer

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Date subtraction not working correctly for the month of February

jyoti kumariFeb 27 2024 — edited Feb 27 2024

Hi All,

I am trying to subtract 35 months from sysdate and calculate the no of days in last 35 months with below query, it is working fine for all the months except for February.

SELECT SUM(EXTRACT(DAY FROM LAST_DAY(ADD_MONTHS(TRUNC(SYSDATE, 'MM'), - LEVEL))))-1 EXPECTED_DAYS FROM DUAL CONNECT BY LEVEL <= 35;

On analyzing further, found that Oracle is considering 29 days for month of February in each year, even though the year is not a leap year. Can someone please suggest what changes should I make in the query to consider the days for February based on whether it's a leap year or not?

Thanks.

Comments
Post Details
Added on Feb 27 2024
4 comments
274 views