I'm trying to get the previous working day in PL SQL. I'm not 100% sure if the way I'm trying to do it is correct.
Essentially we have a holiday table and I'm trying to loop an input date through the holiday table and then return the previous working day. I'm using a cursor to get holiday list and then using a while
loop. The issue I have is that the code runs, but the cursor is only returning the last value in the holiday list.
Create or Replace Function GetPreviousWorkingDay
(
CurrentDate in Date
)
Return Date
As
PreviousWorkingDay Date;
Cursor dt_cursor is
Select holiday_date
from holiday_table;
HolidayDate date;
Begin
PreviousWorkingDay := CurrentDate;
Open dt_cursor;
Loop
Fetch dt_cursor into HolidayDate;
Exit When dt_cursor%NOTFOUND;
End Loop;
While PreviousWorkingDay = HolidayDate
Loop
PreviousWorkingDay := PreviousWorkingDay - 1;
Exit When PreviousWorkingDay <> HolidayDate;
End Loop;
Close dt_cursor;
Return previousworkingday;
END;
any help would be appreciated. Thanks