My function fn_test calculates the date-1. And if it's a holiday according to the table temp_calendar, then it recursively calls the fn_test again to do -1. This happens till I get a non holiday date.
I have implemented as follows:
But can I have a single SQL do it which is more efficient?
Drop table temp_calendar;
Create table temp_calendar(
id number,
holiday date);
Insert into temp_calendar values (1, '5-Jan-2012');
Insert into temp_calendar values (1, '6-Jan-2012');
Insert into temp_calendar values (1, '10-Jan-2012');
Insert into temp_calendar values (1, '2-Feb-2012');
Insert into temp_calendar values (1, '11-Feb-2012');
Commit;
CREATE OR REPLACE FUNCTION fn_test (in_date IN DATE)
RETURN DATE
IS
v_pr_day DATE;
BEGIN
v_pr_day := in_date-1;
FOR Calendar_Dates IN (SELECT holiday FROM temp_calendar)
LOOP
IF (v_pr_day = Calendar_Dates.holiday)
THEN
v_pr_day := fn_test (v_pr_day);
END IF;
END LOOP;
RETURN TRUNC (v_pr_day);
END fn_test;
/
Select fn_test('8-JAN-2012') from dual; -- Returns 7 Jan as no holiday in table temp_calendar.
But Select fn_test('7-JAN-2012') from dual; --Returns 4 Jan as 6 and 5 are holidays in table temp_calendar.
Thanks..