<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Roddy Crossan</title>
        <link>http://geekswithblogs.net/RoddyCrossan/Default.aspx</link>
        <description> </description>
        <language>en-GB</language>
        <copyright>RoddyCrossan</copyright>
        <managingEditor>Crossaro@gmail.com</managingEditor>
        <generator>Subtext Version 0.0.0.0</generator>
        <image>
            <title>Roddy Crossan</title>
            <url>http://geekswithblogs.net/images/RSS2Image.gif</url>
            <link>http://geekswithblogs.net/RoddyCrossan/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <item>
            <title>SQL Server Function to add working days on to a date</title>
            <category>SQL Server</category>
            <link>http://geekswithblogs.net/RoddyCrossan/archive/2009/08/21/sql-server-function-to-add-working-days-on-to-a.aspx</link>
            <description>&lt;p&gt;Originally posted on: &lt;a href='http://geekswithblogs.net/RoddyCrossan/archive/2009/08/21/sql-server-function-to-add-working-days-on-to-a.aspx'&gt;http://geekswithblogs.net/RoddyCrossan/archive/2009/08/21/sql-server-function-to-add-working-days-on-to-a.aspx&lt;/a&gt;&lt;/p&gt;&lt;p&gt;This is my first post so please be nice to me… I have recently been asked to look at automating the recalculation of the SLA target on helpdesk calls which had the clock stopped prior to closure. When I had my first look it seemed nice and simple… you have a number of days to add to the target date so all I would need is a DATEADD() function and kablamo something that was taking a person a day a month today could be done in a matter of seconds.&lt;/p&gt;
&lt;p&gt;However when I went into a bit more detail however it appears that what they wanted was to add on only working days, e.g. if the target goal is 2 days and the target date is on a Friday this should actually recalculate the target date as Tuesday. So this raised the question what is the simplest way to add 2 working days to a date.&lt;/p&gt;
&lt;p&gt;Now I am not saying that my solution is the most elegant solution (generally I try and avoid using functions if I can) in the world but it certainly seemed to work for me… what I decided to do was create a function which would do the work for me, the reason for doing it this way was partially for speed and partially as it could be a fairly useful tool for other things.&lt;/p&gt;
&lt;p&gt;Below is the SQL used to create the function :&lt;/p&gt;
&lt;pre class="brush: sql;"&gt;CREATE FUNCTION AddWorkDays 
(    
    @WorkingDays As Int, 
    @StartDate AS DateTime 
) 
RETURNS DateTime 
AS 
BEGIN 
    DECLARE @Count AS Int 
    DECLARE @i As Int 
    DECLARE @NewDate As DateTime 
    SET @Count = 0 
    SET @i = 0 

    WHILE (@i &amp;lt; @WorkingDays) --runs through the number of days to add 
    BEGIN 
-- increments the count variable 
        SELECT @Count = @Count + 1 
-- increments the i variable 
        SELECT @i = @i + 1 
-- adds the count on to the StartDate and checks if this new date is a Saturday or Sunday 
-- if it is a Saturday or Sunday it enters the nested while loop and increments the count variable 
           WHILE DATEPART(weekday,DATEADD(d, @Count, @StartDate)) IN (1,7) 
            BEGIN 
                SELECT @Count = @Count + 1 
            END 
    END 

-- adds the eventual count on to the Start Date and returns the new date 
    SELECT @NewDate = DATEADD(d,@Count,@StartDate) 
    RETURN @NewDate 
END 
GO&lt;/pre&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Hope it is helpful, it got me by at least :)&lt;/p&gt;
&lt;p&gt;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;/p&gt; &lt;img src="http://geekswithblogs.net/RoddyCrossan/aggbug/134232.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>RoddyCrossan</dc:creator>
            <guid>http://geekswithblogs.net/RoddyCrossan/archive/2009/08/21/sql-server-function-to-add-working-days-on-to-a.aspx</guid>
            <pubDate>Fri, 21 Aug 2009 09:57:05 GMT</pubDate>
            <wfw:comment>http://geekswithblogs.net/RoddyCrossan/comments/134232.aspx</wfw:comment>
            <comments>http://geekswithblogs.net/RoddyCrossan/archive/2009/08/21/sql-server-function-to-add-working-days-on-to-a.aspx#feedback</comments>
            <slash:comments>22</slash:comments>
            <wfw:commentRss>http://geekswithblogs.net/RoddyCrossan/comments/commentRss/134232.aspx</wfw:commentRss>
            <trackback:ping>http://geekswithblogs.net/RoddyCrossan/services/trackbacks/134232.aspx</trackback:ping>
        </item>
    </channel>
</rss>