Thanks for your quick reply. I currently don't have access to a public holidays table, but the 500 points will go for calculating the business days in a given month.
Main Topics
Browse All TopicsHi,
I urgently need to calculate the number of business days in a given month. I realise that there are a number of submissions that calculate weekdays etc, but I specifically need to get the number of business days (in a 5 day working week) in a month, taking into account public holidays if possible.
Thanks.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
take a look at the scripts posted in this thread -this should help you achieve exactly what you want - including adding in holidays....
Calculating Business Days
http://www.thescripts.com/
if you need a function for this: this one counts the working days (mo- fr) for a given date. the calculating is based on
SELECT TO_CHAR(SYSDATE, 'D') FROM dual;
if this returns 2 for monday (6 for friday), it will work (depending on your NLS settings this may vary), otherwise you have to change the code.
CREATE OR REPLACE FUNCTION get_working_days (
p_start_date IN DATE
)
RETURN NUMBER IS
v_last_day NUMBER;
v_cnt NUMBER := 0;
BEGIN
SELECT TO_CHAR(LAST_DAY(p_start_d
INTO v_last_day
FROM dual;
--
FOR i IN 1..v_last_day
LOOP
IF TO_CHAR(p_start_date - 1 + i, 'D') BETWEEN 2 AND 6 THEN
v_cnt := v_cnt + 1;
END IF;
END LOOP;
--
RETURN(v_cnt);
--
END;
What is the source for your "given" month (this will obviously need to include the year as well)? If it is a date field, then jwahl's function will get you what you need although I think it may need a modification to make sure that the date parameter passed in is adjusted to the first day of the month. This can be done changing this
IF TO_CHAR(p_start_date - 1 + i, 'D') ...
to this -
IF TO_CHAR(trunc(p_start_date
If the source is something other than a date field, we can create a function to deal with that as well. For example, if it is character data in the form of 'yyyymm', the following function will work:
create or replace function get_bus_days(yr_mth_in in varchar2) return number is
beg_day date;
end_day date;
tot_days pls_integer;
bus_days pls_integer := 0;
day_of_wk pls_integer;
begin
beg_day := to_date(yr_mth_in,'yyyymm'
end_day := add_months(beg_day,1) - 1;
tot_days := end_day - beg_day;
for i in 0..tot_days
loop
day_of_wk := to_number(to_char(beg_day + i,'D'));
if day_of_wk between 2 and 6 then
bus_days := bus_days + 1;
end if;
end loop;
return bus_days;
end;
/
Here's an adaptation from jwahl's code. It can be easily adapted to a function.
Some holidays are taken into consideration - there are two kinds: fixed date (Independence Day) or variable date (Thanksgiving). You can add your own.
declare
startDate date := sysdate;
ctrDate date ;
numMonth number;
numDay number;
dayCount number := 0;
BEGIN
dbms_output.enable;
ctrDate := trunc(startDate, 'MM');
-- calculate business days in the month
while (ctrDate <= last_day(startDate))
loop
if to_char(ctrDate, 'D') between 2 and 6 then
dayCount := dayCount + 1;
end if;
ctrDate := ctrDate+1;
end loop;
numMonth := to_number(to_char(startDat
numDay := to_number(to_char(startDat
-- eliminate 1/1, 7/4, 12/25
if ((numMonth = 1) and to_number(to_char(to_date(
((numMonth = 7) and to_number(to_char(to_date(
((numMonth = 12) and to_number(to_char(to_date(
then dayCount := dayCount-1;
end if;
-- eliminate variable date public holidays that fall on working days
-- MLK = 3rd Monday in January
-- Memorial = last Monday in May
-- Labor = first Monday in September
-- Thanksgiving = 4th Thursday in November
if numMonth in (1,5,9,11) then
dayCount := dayCount-1;
end if;
dbms_output.put_line('#wor
END;
Just to "muddy the waters" even more on the holiday issue, if January 1 falls on a Saturday, you would need to add an extra "non-business" day to the prior December calculation since the New Year's holiday would be observed on December 31. Also, if Veteran's day is observed, then if numMonth = 11 then dayCount := dayCount - 2;
Good luck!
If I were in your shoes and asked the same to write for such thing, I would the following
Simple example brain-feed for your goal.
Store all the holiday dates in a table (except saturday and sunday general public holidays) with only federal and other state/provincial/corporate
Write a simple function which accepts a month (numeric) parameter and returns the number of days.
Taking the given inputted parameter, I would check the date existence in the holiday list table for the same month and subtract that number of holiday dates existence from the given month and return the result
For example
SELECT myNumDays(4) FROM dual;
FUNCTION myNumDays(month IN integer)
would find out if any dates exist in the holiday list table that are of 4th month and return the count.
That count is subtracted from the number of days of given month, for example April, 30 - 8 (4 * sat/sun) = 24 - number of holiday count for that april month.
The function should be able to find out if a holiday falls on SATURDAY or SUNDAY would exclude from the calculation as SAT and SUNDAYs are already assumed holidays.
Check it out. Good luck :)
Business Accounts
Answer for Membership
by: jwahlPosted on 2007-04-23 at 03:53:36ID: 18957314
do you have a table for the public holidays? they may vary ...