Skip to Main Content

SQL & PL/SQL

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!

interval + '1' [year|month|day] not taking leap years into account?

523861Jul 13 2011 — edited Jul 14 2011
Hi all

Database Version: Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi
Database Version: PL/SQL Release 10.2.0.5.0 - Production

I just realised that the interval function doesn't take leap years into account.
SQL> select to_date('29-02-2008','dd-mm-yyyy') + interval '1' year 
  2    from dual;
select to_date('29-02-2008','dd-mm-yyyy') + interval '1' year
                                      *
ERROR at line 1:
ORA-01839: date not valid for month specified
I've searched through the documentation as best I can and could find no mention of this.

If I want to add one year to a date, do I have to manually calculate the number of days or do something like incrementing only the year portion and then manually verifying that it doesn't fall on the 29th of february if it's not a leap year?

edit:

just to clarify, day and minute appear to work for instance but month and year don't:
SQL> select to_date('28-02-2009','dd-mm-yyyy') + interval '1' day
  2    from dual;

TO_DATE('
---------
01/MAR/09

1 row selected.

Elapsed: 00:00:00.00
SQL> 
SQL> select to_date('28-02-2009 23:59','dd-mm-yyyy hh24:mi') + interval '1' minute 
  2    from dual;

TO_DATE('
---------
01/MAR/09

1 row selected.

Elapsed: 00:00:00.00
SQL> 
SQL> select to_date('29-01-2009','dd-mm-yyyy') + interval '1' month
  2    from dual;
select to_date('29-01-2009','dd-mm-yyyy') + interval '1' month
                                      *
ERROR at line 1:
ORA-01839: date not valid for month specified


Elapsed: 00:00:00.00
SQL> 
SQL> select to_date('29-02-2008','dd-mm-yyyy') + interval '1' year
  2    from dual; 
select to_date('29-02-2008','dd-mm-yyyy') + interval '1' year
                                      *
ERROR at line 1:
ORA-01839: date not valid for month specified
Edited by: WhiteHat on Jul 14, 2011 10:31 AM
This post has been answered by Frank Kulash on Jul 13 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 11 2011
Added on Jul 13 2011
2 comments
3,108 views