Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Finding the Next Business Day Recursively


Finding the Next Business Day Recursively

Author
Message
John McLaughlin-213204
John McLaughlin-213204
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 8

Here's another function that takes a simpler approach. If you are maintaining a holiday table, it would be simple enough to check the return value against it as well.

John

CREATE FUNCTION dbo.udf_GetNextBizDay (@dDateValue DATETIME)
RETURNS Datetime
AS
---------------------------------------------------------------------------------
--
-- Modification History:
--
-- Date Version Programmer Comments
-- ------------------ ------- ---------- ----------------------------
-- 2005-06-13 02:23:00 1.0 John McLaughlin
--
--
BEGIN

DECLARE @dReturnDate DATETIME

IF ISDATE(@dDateValue)=0
BEGIN
RETURN NULL
END

SET @dReturnDate = DATEADD(d,1,@dDateValue)
--Set to zero hour. Uncomment if needed.
--SET @dReturnDate = CAST(CONVERT(CHAR(10),@dReturnDate,101) AS DATETIME)
WHILE DATEPART(dw,@dReturnDate) in(1,7)
BEGIN
SET @dReturnDate = DATEADD(d,1,@dReturnDate)
END


RETURN @dReturnDate
END


Jason1972
Jason1972
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 167

I like the trick for handling the DATEFIRST. With it, you can write the function in 3 lines without recursion, which I think is a bit more elegant.

CREATE FUNCTION dbo.fnGetNextBusinessDay (@Date datetime) RETURNS datetime AS
BEGIN
SET @Date = DATEADD(dd, 1, CONVERT(datetime, (CONVERT(CHAR(10), @Date, 101))))
WHILE ((@@DATEFIRST + DATEPART(dw, @Date) - 1) % 7) + 1 IN (1, 7) OR @Date IN (SELECT HolidayDate FROM Holiday) SET @Date = DATEADD(dd, 1, @Date)
RETURN @Date
END


Ninja's_RGR'us
Ninja's_RGR'us
SSC-Insane
SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)SSC-Insane (22K reputation)

Group: General Forum Members
Points: 22149 Visits: 9671

Hey Joe, don't you feel like you are talking for nothing sometimes???

Guys, Joe literally wrote the book on SQL, he wrote the ISO standards for SQL. I would strongly suggest you listen to him on this one... and basically any other post he makes .

Even if it seems like overkill this time, and the next time and the time after that. Won't you wish you had taken the time to write one when you decid to implement it the nth time... and then decid to make that the business rule to implement overall once you see the benefits??


TriggerMe
TriggerMe
SSC Eights!
SSC Eights! (902 reputation)SSC Eights! (902 reputation)SSC Eights! (902 reputation)SSC Eights! (902 reputation)SSC Eights! (902 reputation)SSC Eights! (902 reputation)SSC Eights! (902 reputation)SSC Eights! (902 reputation)

Group: General Forum Members
Points: 902 Visits: 328

I don't understand the @numDays input parameter!

For example if do this,

DECLARE @Date SMALLDATETIME

SET @Date = dbo.fnGetNextBusinessDay ('2006-12-24', 10)

SELECT @Date

It's suppsoe to return the date 2006-12-27 as that is the next business day.

Can someone explain please?




Kindest Regards,



Hugo Kornelis
Hugo Kornelis
SSCrazy Eights
SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)SSCrazy Eights (8.7K reputation)

Group: General Forum Members
Points: 8744 Visits: 11717

Hi Trigger,

The @numDays parameter is the number of business days to move forward in time.

To get 2006-12-27, you should have used:

SET @Date = dbo.fnGetNextBusinessDay ('2006-12-24', 1)

to get the "first" next business day. Your use of:

SET @Date = dbo.fnGetNextBusinessDay ('2006-12-24', 10)

should get the 10th next business day.




Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
bishop
bishop
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 17
One minor problem in the recursive part of the function
The where clause 'holidayDate=@nextBusDay' will not evaluate
correctly if the @NextbusDay has a time part.
Need to change this to normalize the time part for both dates to 00:00:00 before comparing.


I loved the article though.Good work Rob.
The Danish Dynamo
The Danish Dynamo
Grasshopper
Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)Grasshopper (22 reputation)

Group: General Forum Members
Points: 22 Visits: 69

This did not do it for me:

select dbo.GetNextBusinessDay(getdate(),1)
select dbo.GetNextBusinessDay(getdate()+1,1) -- Fri
select dbo.GetNextBusinessDay(getdate()+2,1) -- Sat
select dbo.GetNextBusinessDay(getdate()+3,1) -- Sun


------------------------------------------------------
2007-04-27 12:41:00

(1 row(s) affected)


------------------------------------------------------
2007-04-30 12:41:00

(1 row(s) affected)


------------------------------------------------------
2007-04-29 12:41:00

(1 row(s) affected)


------------------------------------------------------
2007-04-30 12:41:00

(1 row(s) affected)


Rachel Foppiano
Rachel Foppiano
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 29
I have the same problem as Thomas Nielson, it does not return the right value when the first parameter is a saturday try 3/31/2007, it returns 4/1 when it should return 4/2.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search