with
function birthday (d in date, age in number) return date is
begin
if to_char(d,'DD/MM') = '28/02'
and to_char(add_months(d,age*12),'DD/MM') = '29/02'
then
return add_months(d,age*12)-1;
else
return add_months(d,age*12);
end if;
end;
select * from (
with testdata as (
select date'2000-12-25' as d1
,date'2000-02-29' as d2
,date'2000-02-28' as d3
,date'2001-02-28' as d4
from dual)
select rownum-1 as age
,birthday(d1,rownum-1) as d1
,birthday(d2,rownum-1) as d2
,birthday(d3,rownum-1) as d3
,birthday(d4,rownum-1) as d4
from testdata connect by level <= 12
);
AGE | D1 | D2 | D3 | D4 | 0 | 25-DEC-00 | 29-FEB-00 | 28-FEB-00 | 28-FEB-01 | 1 | 25-DEC-01 | 28-FEB-01 | 28-FEB-01 | 28-FEB-02 | 2 | 25-DEC-02 | 28-FEB-02 | 28-FEB-02 | 28-FEB-03 | 3 | 25-DEC-03 | 28-FEB-03 | 28-FEB-03 | 28-FEB-04 | 4 | 25-DEC-04 | 29-FEB-04 | 28-FEB-04 | 28-FEB-05 | 5 | 25-DEC-05 | 28-FEB-05 | 28-FEB-05 | 28-FEB-06 | 6 | 25-DEC-06 | 28-FEB-06 | 28-FEB-06 | 28-FEB-07 | 7 | 25-DEC-07 | 28-FEB-07 | 28-FEB-07 | 28-FEB-08 | 8 | 25-DEC-08 | 29-FEB-08 | 28-FEB-08 | 28-FEB-09 | 9 | 25-DEC-09 | 28-FEB-09 | 28-FEB-09 | 28-FEB-10 | 10 | 25-DEC-10 | 28-FEB-10 | 28-FEB-10 | 28-FEB-11 | 11 | 25-DEC-11 | 28-FEB-11 | 28-FEB-11 | 28-FEB-12 |
---|
alter session set nls_date_format = 'DD-MON-YYYY'
Statement processed.
with
function birthday (d in date, age in number) return date is
begin
if to_char(d,'DD/MM') = '28/02'
and to_char(add_months(d,age*12),'DD/MM') = '29/02'
then
return add_months(d,age*12)-1;
else
return add_months(d,age*12);
end if;
end;
select * from (
with testdata as (
select date'2000-12-25' as d1
,date'2000-02-29' as d2
,date'2000-02-28' as d3
,date'2001-02-28' as d4
from dual)
select rownum-1 as age
,birthday(d1,rownum-1) as d1
,birthday(d2,rownum-1) as d2
,birthday(d3,rownum-1) as d3
,birthday(d4,rownum-1) as d4
from testdata connect by level <= 12
);
AGE | D1 | D2 | D3 | D4 | 0 | 25-DEC-2000 | 29-FEB-2000 | 28-FEB-2000 | 28-FEB-2001 | 1 | 25-DEC-2001 | 28-FEB-2001 | 28-FEB-2001 | 28-FEB-2002 | 2 | 25-DEC-2002 | 28-FEB-2002 | 28-FEB-2002 | 28-FEB-2003 | 3 | 25-DEC-2003 | 28-FEB-2003 | 28-FEB-2003 | 28-FEB-2004 | 4 | 25-DEC-2004 | 29-FEB-2004 | 28-FEB-2004 | 28-FEB-2005 | 5 | 25-DEC-2005 | 28-FEB-2005 | 28-FEB-2005 | 28-FEB-2006 | 6 | 25-DEC-2006 | 28-FEB-2006 | 28-FEB-2006 | 28-FEB-2007 | 7 | 25-DEC-2007 | 28-FEB-2007 | 28-FEB-2007 | 28-FEB-2008 | 8 | 25-DEC-2008 | 29-FEB-2008 | 28-FEB-2008 | 28-FEB-2009 | 9 | 25-DEC-2009 | 28-FEB-2009 | 28-FEB-2009 | 28-FEB-2010 | 10 | 25-DEC-2010 | 28-FEB-2010 | 28-FEB-2010 | 28-FEB-2011 | 11 | 25-DEC-2011 | 28-FEB-2011 | 28-FEB-2011 | 28-FEB-2012 |
---|