Using a table to identify the US holidays -
Main Topics
Browse All TopicsI need a function in which I pass a date and returns to me the next work day which can't be a saturday, sunday or U.S holidays.
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.
create function getNextWorkDay (@inputDate datetime, @numOfDays int)
returns datetime
as
begin
declare @nextday datetime;
set @nextday = dateadd(d,@numOfDays,@inpu
while (datepart(weekday,@nextday
exists (select 1 from hols_table where hol_date = @nextday))
begin
set @nextday = dateadd(d,@numOfDays,@next
end;
return @nextday
end;
But if in the part of the code be "set @nextday = dateadd(d,@numOfDays,@next
create function getNextWorkDay (@inputDate datetime, @numOfDays int)
returns datetime
as
begin
declare @nextday datetime;
set @nextday = dateadd(d,@numOfDays,@inpu
while (datepart(weekday,@nextday
exists (select 1 from hols_table where hol_date = @nextday))
begin
set @nextday = dateadd(d,1,@nextday);
end;
return @nextday
end;
Or did you meant to jump n working days
create function getNextWorkDay (@inputDate datetime, @numOfDays int)
returns datetime
as
begin
declare @nextday datetime;
declare @calcday int;
set @nextday = dateadd(d,1,@inputdate);
set @calcday = 0;
while (datepart(weekday,@nextday
exists (select 1 from hols_table where hol_date = @nextday) or (@calcday < @numOfDays))
begin
if not ((datepart(weekday,@nextda
set @calcday = @calcday + 1;
end if;
set @nextday = dateadd(d,1,@nextday);
end;
return @nextday
end;
On my second option I forgot to add the "or
exists (select 1 from hols_table where hol_date = @nextday)"
create function getNextWorkDay (@inputDate datetime, @numOfDays int)
returns datetime
as
begin
declare @nextday datetime;
declare @calcday int;
set @nextday = dateadd(d,1,@inputdate);
set @calcday = 0;
while (datepart(weekday,@nextday
exists (select 1 from hols_table where hol_date = @nextday) or (@calcday < @numOfDays))
begin
if not ((datepart(weekday,@nextda
exists (select 1 from hols_table where hol_date = @nextday) then
set @calcday = @calcday + 1;
end if;
set @nextday = dateadd(d,1,@nextday);
end;
return @nextday
end;
I corrected my syntax:
create function getNextWorkDay (@inputDate datetime, @numOfDays int)
returns datetime
as
begin
declare @nextday datetime;
declare @calcday int;
set @nextday = dateadd(d,1,@inputdate);
set @calcday = 0;
while (datepart(weekday,@nextday
exists (select 1 from hols_table where hol_date = @nextday) or (@calcday < @numOfDays))
begin
if not ((datepart(weekday,@nextda
exists (select 1 from hols_table where hol_date = @nextday) or (@calcday < @numOfDays)) then
set @calcday = @calcday + 1;
set @nextday = dateadd(d,1,@nextday);
end;
return @nextday
end;
Would'nt the script below do the same thing as the above? Once this one below will execute faster. If not what's the difference?
create function getNextWorkDay (@inputDate datetime, @numOfDays int)
returns datetime
as
begin
declare @nextday datetime;
set @nextday = dateadd(d,@numOfDays,@inpu
while (datepart(weekday,@nextday
exists (select 1 from hols_table where hol_date = @nextday))
begin
set @nextday = dateadd(d,1,@nextday);
end;
return @nextday
end;
Ok one last comment/question. In my holidays table the column "holiday_date" has the holiday's date which also includes the year in it. In the function when it compares "exists (select 1 from hols_table where hol_date = @nextday)" it will never be a holiday if the year doesn't match. Am I right ? If so how can I change it to work correctly?
So I can close this question I just need to figure out why is the function not working. One thing I noticed is that in my holidays table the time will always be "00:00:00.000" so in the function when it compares the datetime with the one in my holidays table it will have some time in it. Because of this it's not comparing correctly at "(select 1 from hols_table where hol_date = @nextday)". How do I change to compare only the date ??
Not quite sure what your are asking in the "it will never be a holiday if the year doesn't match" post. Some holidays are moveable, don't fall on the same date from year to year, eg Easter, so it is meaningless to check the date without a year part.
if your input date value has a time component, you can eliminate the time part using:
With all the information I came up with the script below. But I found that when adding one day to the date it's also counting the weekend and holiday. For example if I pass ('2009-02-11 17:10:00.000', 7) it will return me the date "2009-02-18 00:00:00.000" but the correct counting ONLY bussiness day it has to return "2009-02-20 00:00:00.000". Can anyone help me to correct the script ??
Business Accounts
Answer for Membership
by: gkernPosted on 2009-01-08 at 02:12:34ID: 23323475
There is no simple function for that. You should create a table that will hold the holidays.
Then when the nextWorkingDay exists in the table ask for the next day and do the same till you get the next day that not exist in the table. You can do that either within the nextWorkingDay function or do it explictly.
CREATE FUNCTION getNextWorkDay (@inputDate DATETIME)
RETURNS DATETIME
AS
BEGIN
DECLARE @aDay INT
DECLARE @nextDay DATETIME
SET @aDay = DATEPART(weekday,@inputDat
--To find Previous working day
IF @aDay = 6
SET @nextDay = DATEADD(d,3,@inputDate)
ELS
SET @nextDay = DATEADD(d,2,@inputDate)
ELS
SET @nextDay = DATEADD(d,1,@inputDate)
RET
END