Job Search

Sunday, November 15, 2015

calculate workdays between two dates

create holidays table which contains dates of holiday's
create table holidays
(holiday varchar2(25),
On_date date
)

CREATE OR REPLACE FUNCTION weekdays_between (in_date1 IN DATE, in_date2 IN DATE)
RETURN NUMBER
IS
oldest_date DATE;
newest_date DATE;
days_between NUMBER := 0;

BEGIN
oldest_date := TRUNC(LEAST(in_date1, in_date2));
newest_date := TRUNC(GREATEST(in_date1, in_date2));

LOOP
EXIT WHEN oldest_date = newest_date;

IF to_char(oldest_date,'DY') NOT IN ('SAT','SUN') AND (check oldest_date not in the holidays table) THEN
days_between := days_between +1;
END IF;

oldest_date := oldest_date + 1;

END LOOP;

RETURN days_between;

END;
/

I hope you all have enjoyed reading this article. Comments are welcome....

No comments:

Post a Comment