﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Discuss Content Posted by Rob Scholl / Article Discussions / Article Discussions by Author  / Finding the Next Business Day Recursively / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 23 May 2013 14:17:29 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>Just to add another version - I used Jason's code and added a parm for number of days to add, plus or minus:[code][font="Courier New"]CREATE FUNCTION dbo.GetNextBusinessDay (@Date datetime,                                        @DaysToAdd int)RETURNS datetime/* Name:    GetNextBusinessDay * * Purpose: Calculate business days from a particular date.  @DaysToAdd can be positive or negative. * * Modification History: *  03/20/2009  VRI  Derived from http://www.sqlservercentral.com/Forums/Topic228794-253-2.aspx "Jason" * */ASBEGIN    DECLARE @DayCounter int,            @Sign int    SET @DayCounter = 0     SET @Sign = SIGN(@DaysToAdd)    WHILE (@DayCounter &amp;lt; ABS(@DaysToAdd))    BEGIN        SET @Date = DATEADD(dd, @Sign, 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, @Sign, @Date)                SET @DayCounter = @DayCounter + 1    END        RETURN @DateEND[/font][/code]</description><pubDate>Fri, 20 Mar 2009 06:52:07 GMT</pubDate><dc:creator>viacoboni</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>without disagreeing with any of the above, I got bit by having a "list of holidays" table yesterday.We have a table, unimaginatively called ms_holiday.The structure is similar to [code]CREATE TABLE [dbo].[MS_Holiday] (	[Holiday_Depot_No] [varchar] (7) COLLATE Latin1_General_CI_AI NOT NULL ,	[Holiday_Dt] [datetime] NOT NULL ,	[Holiday_Desc] [varchar] (40) COLLATE Latin1_General_CI_AI NOT NULL ) ON [SECONDARY][/code](I snipped out the created_by, created_date, constraints etc.). This stores holidays for each depot in our organization, so we could store Western Easter dates at out London depots, and Orthodox Easter dates at our Greek(?) Depots . ;)But it breaks down when we add new depots and forget to populate the table with the new depots and their corresponding dates! [Yes, it was me, guilty as charged... :blush:]Therefore working things out on the fly for the times that these functions are called sounds a better bet for me.  Mind you, I could always go and beat up the developers of the app until they write a complete add new depot screen.  (There are so many hacks I weep, regularly :()Dave JEdited: Took out a typo or two.  How come you never spot them in the preview, only after post?</description><pubDate>Wed, 20 Feb 2008 07:42:55 GMT</pubDate><dc:creator>David Jackson</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>It certainly is - but in both cases - it just goes to show how unwieldy it would get to calculate every single holiday based on some crazy set of rules.  If you're going to go through all of that trouble - calculate it and store it (never mind the bizarre add-on rules, like "observe the holiday on the monday if it falls on a sunday, except if it's a minor holiday which might be defined by theses x rules in the HR handbook...")And Joe's point is a good one.  there are multiple definitions of a lot of these....But even just using the definitions your org has picked out, seems silly to have to figure them out over and over again.</description><pubDate>Thu, 07 Feb 2008 11:18:38 GMT</pubDate><dc:creator>Matt Miller (#4)</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>[size="3"][font="Courier New"]&amp;gt;&amp;gt; While I fully agree with Joe that it becomes unwieldy without the dates table &amp;lt;&amp;lt;I would say impossible because of civic decrees, which are unpredictable.  And a 100 year table is really small by today's standards.&amp;gt;&amp;gt; - there IS an algorithm that calculates Easter....Thought you might be interested. &amp;lt;&amp;lt;Which Easter ?:) Here is the Greek Orthodox versus Roman Catholic and Protestant versions (please pardon the lack of ISO-8601 formatting on the dates):      WESTERN            ORTHODOX     ~~~~~~~         ~~~~~~~~      23 April 2000      30 April 2000      15 April 2001      15 April 2001      31 March 2002     5  May  2002      20 April 2003      27 April 2003      11 April 2004      11 April 2004      27 March 2005     1  May  2005      16 April 2006      23 April 2006       8 April 2007        8 April 2007      23 March 2008    27 April 2008      12 April 2009      19 April 2009         etc. Now, I have a question.  Is there an international standard for holidays?  Some Dewey Decimal kind of thing that breaks them into civic, religious, etc. categories or something?  I know the freighter ship industry has a list  by country and ports of holidays, but that is all I have seen.  </description><pubDate>Thu, 07 Feb 2008 11:15:15 GMT</pubDate><dc:creator>CELKO</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>[quote][b]Matt Miller (2/7/2008)[/b][hr]While I fully agree with Joe that it becomes unwieldy without the dates table - there IS an algorithm that calculates Easter....Thought you might be interested.[url=http://users.sa.chariot.net.au/~gmarts/eastalg.htm]http://users.sa.chariot.net.au/~gmarts/eastalg.htm[/url][/quote]Matt, While I also agree there is a SQL Holiday Algorithm referred to here.  I think it is a corker...[url=http://www.sqlservercentral.com/Forums/Topic110259-23-1.aspx]http://www.sqlservercentral.com/Forums/Topic110259-23-1.aspx[/url]Dave J</description><pubDate>Thu, 07 Feb 2008 11:09:27 GMT</pubDate><dc:creator>David Jackson</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>While I fully agree with Joe that it becomes unwieldy without the dates table - there IS an algorithm that calculates Easter....Thought you might be interested.[url=http://users.sa.chariot.net.au/~gmarts/eastalg.htm]http://users.sa.chariot.net.au/~gmarts/eastalg.htm[/url]</description><pubDate>Thu, 07 Feb 2008 10:25:15 GMT</pubDate><dc:creator>Matt Miller (#4)</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>This simply does not work.  There is no way to compute all the holiday in the Common Era Calendar.  Where is your calculation for Easter? Chinese New Year? etc. You have to use an auxiliary Calendar table with the business days in it.  Google around for details. </description><pubDate>Thu, 07 Feb 2008 09:42:36 GMT</pubDate><dc:creator>CELKO</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>I was looking for a quick way to find the 5th business day of each month and this function handles it pretty well, except I have to find it from the end of last month, it seems, rather than the first of this month.I used the following and it works pretty consistently so far, but I'm still testing.declare @EndOfLastMonth datetimeselect @EndOfLastMonth = dateadd(dd,-day(getdate()),convert(varchar(10),getdate(),120))select dbo.fnGetNextBusinessDay (@EndOfLastMonth, 5)Thanks Rob!  This made a several-hours task into a several-minutes one.</description><pubDate>Thu, 07 Feb 2008 08:44:49 GMT</pubDate><dc:creator>Joe Christley</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>There is no need for recursion.  I am going to assume that you have a Sequence auxiliary table with enough rows for your needs -- say integers 1 to 50,000.   -- create Weekdays tableCREATE TABLE Weekdays(cal_date DATETIME NOT NULL PRIMARY KEY,  julian_date INTEGER NOT NULL);--julianize weekdays for, say, ten years INSERT INTO WeekdaysSELECT DATEADD(DD, seq, '2000-01-01'), seq  FROM Sequence WHERE DATEADD(DD, seq, '2000-01-01') &lt;= '2010-01-01'   AND DATENAME(DW, DATEADD(DD, seq, '2000-01-01'))        NOT IN ('Sunday', 'Saturday'); --julianize weekendsINSERT INTO Weekdays -- add SaturdaysSELECT DATEADD(DD, 1, cal_date), julian_date   FROM Weekdays  WHERE DATENAME(DW, cal_date) = 'Friday';INSERT INTO Weekdays -- add SundaysSELECT DATEADD(DD, 1, cal_date), julian_date   FROM Weekdays  WHERE DATENAME(DW, cal_date) = 'Saturday';Get a spreadsheet and build a list of company holidays for the years we are worried about.  Create a script with calls to a procedure that re-numbers the Julianized days: CREATE PROCEDURE AddHoliday (@new_holiday DATETIME)ASUPDATE Weekdays -- should now be business days   SET julian_date = julian_date - 1 WHERE cal_date &gt;= @new_holiday; you can also write a RemoveHolidsay (@old_holiday DATETIME) procedure in case we drop one (remember when we consolidated Lincoln and Washington's birthdays?) </description><pubDate>Thu, 20 Sep 2007 22:30:00 GMT</pubDate><dc:creator>CELKO</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>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.</description><pubDate>Wed, 19 Sep 2007 10:07:00 GMT</pubDate><dc:creator>Rachel Foppiano</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>&lt;P&gt;This did not do it for me:&lt;/P&gt;&lt;P&gt;select dbo.GetNextBusinessDay(getdate(),1)select dbo.GetNextBusinessDay(getdate()+1,1) -- Friselect dbo.GetNextBusinessDay(getdate()+2,1) -- Satselect dbo.GetNextBusinessDay(getdate()+3,1) -- Sun&lt;/P&gt;&lt;P&gt;                                                      ------------------------------------------------------ 2007-04-27 12:41:00&lt;/P&gt;&lt;P&gt;(1 row(s) affected)&lt;/P&gt;&lt;P&gt;                                                       ------------------------------------------------------ 2007-04-30 12:41:00&lt;/P&gt;&lt;P&gt;(1 row(s) affected)&lt;/P&gt;&lt;P&gt;                                                       ------------------------------------------------------ 2007-04-29 12:41:00&lt;/P&gt;&lt;P&gt;(1 row(s) affected)&lt;/P&gt;&lt;P&gt;                                                       ------------------------------------------------------ 2007-04-30 12:41:00&lt;/P&gt;&lt;P&gt;(1 row(s) affected)&lt;/P&gt;</description><pubDate>Thu, 26 Apr 2007 10:43:00 GMT</pubDate><dc:creator>The Danish Dynamo</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>One minor problem in the recursive part of the functionThe where clause 'holidayDate=@nextBusDay' will not evaluatecorrectly 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.</description><pubDate>Fri, 05 Jan 2007 10:41:00 GMT</pubDate><dc:creator>bishop</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>&lt;P&gt;Hi Trigger,&lt;/P&gt;&lt;P&gt;The @numDays parameter is the number of business days to move forward in time.&lt;/P&gt;&lt;P&gt;To get 2006-12-27, you should have used:&lt;/P&gt;&lt;P&gt;SET&lt;FONT size=2&gt; @Date &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt; dbo&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;FONT size=2&gt;fnGetNextBusinessDay &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'2006-12-24'&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt; 1&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;)&lt;/P&gt;&lt;/FONT&gt;&lt;P&gt;to get the "first" next business day. Your use of:&lt;/P&gt;&lt;P&gt;SET&lt;FONT size=2&gt; @Date &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt; dbo&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;FONT size=2&gt;fnGetNextBusinessDay &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'2006-12-24'&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt; 10&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;)&lt;/P&gt;&lt;/FONT&gt;&lt;P&gt;should get the 10th next business day.&lt;/P&gt;</description><pubDate>Fri, 10 Nov 2006 01:15:00 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>&lt;P&gt;I don't understand the @numDays input parameter!&lt;/P&gt;&lt;P&gt;For example if do this,&lt;/P&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;DECLARE&lt;/FONT&gt;&lt;FONT size=2&gt; @Date &lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;SMALLDATETIME&lt;/FONT&gt;&lt;FONT size=2&gt; &lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;SET&lt;/FONT&gt;&lt;FONT size=2&gt; @Date &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;=&lt;/FONT&gt;&lt;FONT size=2&gt; dbo&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;.&lt;/FONT&gt;&lt;FONT size=2&gt;fnGetNextBusinessDay &lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;(&lt;/FONT&gt;&lt;FONT color=#ff0000 size=2&gt;'2006-12-24'&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;,&lt;/FONT&gt;&lt;FONT size=2&gt; 10&lt;/FONT&gt;&lt;FONT color=#808080 size=2&gt;)&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;&lt;P&gt;SELECT&lt;/FONT&gt;&lt;FONT size=2&gt; @Date&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size=2&gt;It's suppsoe to return the date 2006-12-27 as that is the next business day.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT size=2&gt;Can someone explain please?&lt;/P&gt;&lt;/FONT&gt;</description><pubDate>Thu, 09 Nov 2006 17:57:00 GMT</pubDate><dc:creator>TriggerMe</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>&lt;P&gt;Hey Joe, don't you feel like you are talking for nothing sometimes???&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;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 &lt;img src='images/emotions/smile.gif' height='20' width='20' border='0' title='Smile' align='absmiddle'&gt;.&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;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??&lt;/P&gt;</description><pubDate>Fri, 03 Nov 2006 09:58:00 GMT</pubDate><dc:creator>Ninja's_RGR'us</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;CREATE FUNCTION dbo.fnGetNextBusinessDay (@Date datetime) RETURNS datetime ASBEGIN     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 @DateEND&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Fri, 03 Nov 2006 09:34:00 GMT</pubDate><dc:creator>Jason-262847</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>It is a cute trick, but you would never use it in the real world.  A calendar table is easier, cleaner, portable and faster than this procedural code.  We avoid functions in SQL in favor of a data-driven solution -- it is not a computational language.  </description><pubDate>Fri, 03 Nov 2006 07:38:00 GMT</pubDate><dc:creator>CELKO</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>&lt;P&gt;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.&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;John&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;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 &lt;/P&gt;&lt;P&gt; DECLARE @dReturnDate DATETIME &lt;/P&gt;&lt;P&gt; 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 &lt;/P&gt;&lt;P&gt; RETURN @dReturnDateEND&lt;/P&gt;</description><pubDate>Fri, 04 Nov 2005 07:05:00 GMT</pubDate><dc:creator>John McLaughlin-213204</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>&lt;P&gt;Rob- I like this example. It will work for our business because we don't take long holidays &lt;img src='images/emotions/crazy.gif' height='20' width='20' border='0' title='Crazy' align='absmiddle'&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="Courier New"&gt;Thanks, Matt&lt;/FONT&gt;&lt;/P&gt;</description><pubDate>Thu, 03 Nov 2005 09:32:00 GMT</pubDate><dc:creator>mdolan1959</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>&lt;P&gt;Here's another version of the Easter Calculation algorithm:&lt;/P&gt;&lt;P&gt;&lt;A href="http://sqlservercentral.com/cs/blogs/michael_coles/archive/2005/07/16/49.aspx"&gt;http://sqlservercentral.com/cs/blogs/michael_coles/archive/2005/07/16/49.aspx&lt;/A&gt;&lt;/P&gt;&lt;P&gt;And something for calculating "floating" American holidays:&lt;/P&gt;&lt;P&gt;&lt;A href="http://sqlservercentral.com/cs/blogs/michael_coles/archive/2005/07/17/52.aspx"&gt;http://sqlservercentral.com/cs/blogs/michael_coles/archive/2005/07/17/52.aspx&lt;/A&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Thu, 03 Nov 2005 09:14:00 GMT</pubDate><dc:creator>Mike C</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>&lt;P&gt;Paul Cresham posted an elegant solution for this including working out holidays on the fly, albeit for the UK.  It did contain a nifty Easter algorithm, as well as tackling the @@DATEFIRST problem.  see &lt;/P&gt;&lt;P&gt;&lt;A href="http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=23&amp;amp;messageid=110259"&gt;http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=23&amp;amp;messageid=110259&lt;/A&gt;&lt;/P&gt;&lt;P&gt;I documented it with an example of how we use it at &lt;/P&gt;&lt;P&gt;&lt;A href="http://glossopian.co.uk/pmwiki.php?n=Main.WorkingDays"&gt;http://glossopian.co.uk/pmwiki.php?n=Main.WorkingDays&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Dave Jackson&lt;/P&gt;</description><pubDate>Thu, 03 Nov 2005 05:50:00 GMT</pubDate><dc:creator>David Jackson</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>&lt;P&gt;One thing to note. &lt;/P&gt;&lt;P&gt;@@Datefirst is affected by things like the defaultlanguage property of your language.&lt;/P&gt;&lt;P&gt;I believe that for the US @@Datefirst defaults to 7, whereas if you set up your login to use British English, as we brits often do, it will be 1.&lt;/P&gt;&lt;P&gt;This will affect what Datepart(weekday, getdate()) returns.&lt;/P&gt;&lt;P&gt;Just something to be aware of, I've had problems with this before. &lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Thu, 03 Nov 2005 05:38:00 GMT</pubDate><dc:creator>Mark Hickin</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>&lt;P&gt;Cute, but I agree with Ian, if you are using a table why not maintain the correct dates in the table, it is MUCH easier to work with.&lt;/P&gt;&lt;P&gt;I did not know the 32 levels of recursion limitation, thanks &lt;img src='images/emotions/wow.gif' height='20' width='20' border='0' title='Wow' align='absmiddle'&gt;&lt;/P&gt;</description><pubDate>Thu, 03 Nov 2005 03:22:00 GMT</pubDate><dc:creator>Mark Firth-200666</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>&lt;P&gt;Elegant solution (I too like the trick to get the correct day regardless of datefirst!  Clever!).  BUT...  recursion in SQL is limited to 32 calls (XMAS holidays in schools, for example, go a lot longer than this) - it could easily have been done with a simple loop, or better yet, a simple select statement...&lt;/P&gt;&lt;P&gt;In our software where we have to keep track of days a clinic is open for business, rather than messing around with such procedures, we just have a dates table with one value for each day and a bit flag representing opened or closed...  Makes reporting VERY easy and you can join to the table easily to group days together efficiently, etc.  For similar ideas and more info on why just storing the data in a table can be efficient, look up "numbers table" on this site - Adam Machanic has quite a bit written about them.&lt;/P&gt;&lt;P&gt;But I still like that modulus trick!! &lt;img src='images/emotions/tongue.gif' height='20' width='20' border='0' title='Tongue' align='absmiddle'&gt;&lt;/P&gt;</description><pubDate>Thu, 03 Nov 2005 03:07:00 GMT</pubDate><dc:creator>Ian Yates</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>&lt;P&gt;Hi Rob,&lt;/P&gt;&lt;P&gt;While the function uses some nifty tricks (I particularly like the trick to get correct results regardless of datefirst setting; this is a new trick for me), I'd never recommend using this function to anyone.&lt;/P&gt;&lt;P&gt;There are other ways to achieve this. Ways that are, in my opinion, better. Check out &lt;A href="http://www.aspfaq.com/show.asp?id=2519"&gt;http://www.aspfaq.com/show.asp?id=2519&lt;/A&gt;, and especially the section titled "&lt;STRONG&gt;Pre-determine delivery dates&lt;/STRONG&gt;".&lt;/P&gt;&lt;P&gt;Best, Hugo&lt;/P&gt;</description><pubDate>Thu, 03 Nov 2005 02:13:00 GMT</pubDate><dc:creator>Hugo Kornelis</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>&lt;P&gt;Rob,&lt;/P&gt;&lt;P&gt;  for some reason your function didn't work for me. I changed it a bit to get it working here and changed basically two things:&lt;/P&gt;&lt;P&gt;1) As we are using recursion there is no need for an explicit loop inside the function. The iteration is done using the consecutive calls to the function.&lt;/P&gt;&lt;P&gt;2) I get rid of the HH:MM:SS of the startDate in order to compare with the dates on the Holiday table. Otherwise the comparison would be like '2005-11-07 16:41:03' = '2005-11-07 00:00:00', which would evaluate to FALSE.&lt;/P&gt;&lt;P&gt;Below is the code with the changes.&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt;Andre&lt;/P&gt;&lt;P&gt;create function fnGetNextBusinessDay (@startDate smalldatetime) returns smalldatetime as Begin Declare @nextBusDay smalldatetime Declare @weekDay tinyInt set @nextBusDay = convert(datetime,left(convert(varchar,@startDate + 1,120),10),120) -- first get the raw next day SET @weekDay =((@@dateFirst+datePart(dw,@nextBusDay)-2) % 7) + 1 if @weekDay in (6, 7) or exists (select 1 from holiday where holidayDate = @nextBusDay) set @nextBusDay = dbo.fnGetNextBusinessDay(@nextBusDay) return (@nextBusDay) End &lt;/P&gt;</description><pubDate>Thu, 03 Nov 2005 00:56:00 GMT</pubDate><dc:creator>Andre Araujo-272450</dc:creator></item><item><title>RE: Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>&lt;P&gt;I have to quibble a bit on the definition of recursion and the article provides a hint to support this.&lt;/P&gt;&lt;P&gt;"Way back when", recursion was taught as "a function which calls itself with parameters|data|whatever in a simpler version of themselves [compared to what was passed in].  Eventually, you will reach a base or termination case which will cause the recursion to unravel itself and provide a solution.&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Thu, 03 Nov 2005 00:40:00 GMT</pubDate><dc:creator>Phil Paxton</dc:creator></item><item><title>Finding the Next Business Day Recursively</title><link>http://www.sqlservercentral.com/Forums/Topic228794-253-1.aspx</link><description>Comments posted to this topic are about the content posted at &lt;A HREF="http://www.sqlservercentral.com/columnists/rScholl/findingthenextbusinessdayrecursively.asp"&gt;http://www.sqlservercentral.com/columnists/rScholl/findingthenextbusinessdayrecursively.asp&lt;/A&gt;</description><pubDate>Thu, 13 Oct 2005 13:03:00 GMT</pubDate><dc:creator>Rob Scholl</dc:creator></item></channel></rss>