Function to adjust weekend and Holiday in Oracle SQL

Hello,

I have a requirement to calculate due date based on the condition below. Could someone please assist on how to create a function?

Thank you


AdjustDtForFedHoliday (Input Date, Type) {      
If Type = Forward {
             If the Input Date falls on weekend then
                   Move the date to following Monday.
Else
Keep the date as is.
If derived date after weekend calculation is a federal holiday (Check if the date is in the table Holiday)
Move the date one day forward and perform the check again to make sure adjusted date is not a weekend or federal holiday.
Else
Keep the date as is.
}
If AdjustmentType = Backward {
If the Input Date falls on weekend then
                   Move the date to Friday before.
Else
Keep the date as is.
If derived date after weekend calculation is a federal holiday (Check if the date is in the table Holiday )
Move the date one day back and perform the check again to make sure adjusted date is not a weekend or federal holiday.
Else
Keep the date as is.
      }}
angel7170Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

flow01Commented:
You probably wil have to adjust the reference to the holidays. I assumed
CREATE TABLE HOLIDAYS (holiday date);
I chose for a straight forward solution that  wil not be the fastest . Try if its fast enough for you.
You can tweek the max .

CREATE OR REPLACE FUNCTION AdjustDtForFedHoliday (Input Date, Type VARCHAR2)
RETURN VARCHAR2
IS
  max_holidays pls_integer := 31;
  v_return_date date;
BEGIN
If Type = 'Forward'  THEN
  SELECT MIN(NEXT_DAY)
  INTO v_return_date
  FROM
  (SELECT input + (level - 1) NEXT_DAY
   FROM DUAL CONNECT BY LEVEL < max_holidays) NEXT_DAYS
   WHERE not TO_CHAR(NEXT_DAY,'DY','NLS_DATE_LANGUAGE = AMERICAN') IN ('SAT','SUN')  -- omit weekend
  --AND NOT EXISTS (SELECT 1 FROM HOLIDAYS WHERE TRUNC(HOLIDAYS.HOLIDAY) = TRUNC(NEXT_DAYS.NEXT_DAY));  -- trunc omits hours etc.. from input
  AND NOT EXISTS (SELECT 1 FROM HOLIDAYS WHERE HOLIDAYS.HOLIDAY = NEXT_DAYS.NEXT_DAY)
;
ELSE
  SELECT MAX(PREVIOUS_DAY)
  INTO v_return_date
  FROM
  (SELECT input - (level - 1) PREVIOUS_DAY
   FROM DUAL CONNECT BY LEVEL < max_holidays) PREVIOUS_DAYS
   WHERE not TO_CHAR(PREVIOUS_DAY,'DY','NLS_DATE_LANGUAGE = AMERICAN') IN ('SAT','SUN')  -- omit weekend
  --AND NOT EXISTS (SELECT 1 FROM HOLIDAYS WHERE TRUNC(HOLIDAYS.HOLIDAY) = TRUNC(PREVIOUS_DAYS.PREVIOUS_DAY));  -- trunc omits hours etc.. from input
  AND NOT EXISTS (SELECT 1 FROM HOLIDAYS WHERE HOLIDAYS.HOLIDAY = PREVIOUS_DAYS.PREVIOUS_DAY)
;
END IF;
RETURN v_return_date;
END;
/

with this_years_dates
as
(select trunc(SYSDATE,'YYYY') + (level - 1)  input_day
 FROM DUAL CONNECT BY LEVEL < 400
)
SELECT input_day,  TO_CHAR(input_DAY,'DY','NLS_DATE_LANGUAGE = AMERICAN') , AdjustDtForFedHoliday(input_day,'Forward') fday,  AdjustDtForFedHoliday(input_day,'Backward') bday
from this_years_dates
/

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
slightwv (䄆 Netminder) Commented:
Sorry.  My code had a bug so I deleted it.  It was almost exactly what flow01 had posted.  

I purposely didn't look at their code before writing mine.  I should have looked at it before posting...  sorry.
angel7170Author Commented:
This is exactly what I looking for. Thank you so much
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.