|
CREATE OR REPLACE function SYSTEM.my_add_months(p_date_string varchar2, p_months number) return varchar2 IS
t int:=p_date_string;
l int:=100;
y int:= t/l/l; m int:= mod(t/l,l); d int:= mod(t,l);
BEGIN
t := case when m=2 and d>=28 then case when not ((mod(y,4)=0 and mod(y,100)!=0 or mod(y,400)=0) and d=28) then 1 end
when m in(4,6,9,11) and d=30 then 1 when d=31 then 1 end;
m := m+p_months;
y := trunc(y+(m-1)/12);
m := mod(m,12);
if m<=0 then m:=m+12; end if;
l:=28;
if mod(y,4)=0 and (mod(y,100)!=0 or mod(y,400)=0) then l:=29; end if;
d := case when m in(4,6,9,11) and (d>30 or t=1) then 30
when m=2 and (d>l or t=1) then l
when t=1 then 31
else d end;
RETURN y||lpad(m,2,'0')||d;
END;
/
SQL> call t();
Congratulation ... Code Length: 555 Bytes. Times: 00:00:03
走hotiice的思路,少4个字节,不过这已经到26号了
CREATE OR REPLACE function SYSTEM.my_add_months(p_date_string varchar2, p_months number) return varchar2 IS
t int:=p_date_string;
l int:=100;
y int:= t/l/l; m int:= mod(t/l,l); d int:= mod(t,l);
BEGIN
t := case when m=2 and d>=28 then case when not ((mod(y,4)=0 and mod(y,25)!=0 or mod(y,16)=0) and d=28) then 1 end
when m in(4,6,9,11) and d=30 then 1 when d=31 then 1 end;
m := m+p_months;
y := trunc(y+(m-1)/12);
m := mod(m,12);
if m<=0 then m:=m+12; end if;
l:=28;
if mod(y,4)=0 and (mod(y,25)!=0 or mod(y,16)=0) then l:=29; end if;
d := case when m in(4,6,9,11) and (d>30 or t=1) then 30
when m=2 and (d>l or t=1) then l
when t=1 then 31
else d end;
RETURN y||lpad(m,2,'0')||d;
END;
/
SQL> call t();
Congratulation ... Code Length: 551 Bytes. Times: 00:00:03 |
|