SQL>
SQL> DECLARE
2 FUNCTION my_add_months (date_in IN DATE, months_shift IN NUMBER)
3 RETURN DATE IS date_out DATE;
4 day_in NUMBER;
5 day_out NUMBER;
6 BEGIN
7 date_out := ADD_MONTHS(date_in, months_shift);
8 day_in := TO_NUMBER(TO_CHAR(date_in,'DD'));
9 day_out := TO_NUMBER(TO_CHAR(date_out,'DD'));
10 IF day_out > day_in
11 THEN
12 date_out := date_out - (day_out - day_in);
13 END IF;
14
15 RETURN date_out;
16 END;
17 BEGIN
18 DBMS_OUTPUT.PUT_LINE(TO_CHAR(my_add_months(
19 TO_DATE('31-Jan-2002 13:14:15','dd-mon-yyyy hh24:mi:ss'),1),
20 'dd-Mon-yyyy hh24:mi:ss'));
21
22 DBMS_OUTPUT.PUT_LINE(TO_CHAR(my_add_months(
23 TO_DATE('28-Feb-2002 13:14:15','dd-mon-yyyy hh24:mi:ss'),1),
24 'dd-Mon-yyyy hh24:mi:ss'));
25 END;
26 /
PL/SQL procedure successfully completed.
SQL>
SQL>
|