<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>AaronTerry.com &#187; SQL</title>
	<atom:link href="http://www.aaronterry.com/category/programming/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.aaronterry.com</link>
	<description>Aaron Terry, Web Developer - Austin, TX</description>
	<lastBuildDate>Mon, 12 Dec 2011 02:20:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>T-SQL: Calculating number of weekdays between two dates</title>
		<link>http://www.aaronterry.com/2009/06/calculating-number-of-weekdays-between-two-dates/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://www.aaronterry.com/2009/06/calculating-number-of-weekdays-between-two-dates/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 05:20:08 +0000</pubDate>
		<dc:creator>acterry</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.aaronterry.com/?p=13</guid>
		<description><![CDATA[Tonight someone tweeted trying to find a ColdFusion or SQL solution to counting the number of weekdays (excluding Saturdays and Sundays) between two dates.  Most of the solutions online involving looping over the date range and adding to a counter if that iteration of the loop is a weekday.

That's fine for small ranges, but not if you need to test dates that span many years.  Anyway, here's a SQL solution.  It's late, so hopefully the comments in the code suffice.  It should be straightforward to rewrite this in ColdFusion.
]]></description>
			<content:encoded><![CDATA[<p>Tonight someone tweeted trying to find a ColdFusion or SQL solution to counting the number of weekdays (excluding Saturdays and Sundays) between two dates.  Most of the solutions online involving looping over the date range and adding to a counter if that iteration of the loop is a weekday.</p>
<p>That&#8217;s fine for limited, but probably not if you need to test dates that span many years or need to run the function 1,000s of times.  Anyway, here&#8217;s a SQL Server UDF solution.  It&#8217;s late, so hopefully the comments in the code suffice.  It should be straightforward to rewrite this in ColdFusion.</p>
<p><span id="more-13"></span></p>
<p>This is an improved version of the solution presented <a href="http://chrismay.org/2008/03/30/Excluding+Weekends+From+A+SQL+Date+Range.aspx" target="_blank">here</a>.  That solution fails when the timespan is less than 7 days long and contains at least one weekend day.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">FUNCTION</span> <span style="color: #66cc66;">&#91;</span>dbo<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">.</span><span style="color: #66cc66;">&#91;</span>dateDiffWeekdays<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&#40;</span>
@startdaytime DATETIME;
@enddaytime DATETIME;
<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">RETURNS</span> <span style="color: #993333; font-weight: bold;">INT</span>
<span style="color: #993333; font-weight: bold;">AS</span>
<span style="color: #993333; font-weight: bold;">BEGIN</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">DECLARE</span> @answer <span style="color: #993333; font-weight: bold;">INT</span>;
<span style="color: #993333; font-weight: bold;">SET</span> @answer <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- Strip Times</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">START</span> <span style="color: #66cc66;">=</span> dateadd<span style="color: #66cc66;">&#40;</span>dd<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> datediff<span style="color: #66cc66;">&#40;</span>dd<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span>@startdaytime<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> @<span style="color: #993333; font-weight: bold;">END</span> <span style="color: #66cc66;">=</span> dateadd<span style="color: #66cc66;">&#40;</span>dd<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span> datediff<span style="color: #66cc66;">&#40;</span>dd<span style="color: #66cc66;">,</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">,</span>@enddaytime<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- handle end conditions</span>
<span style="color: #993333; font-weight: bold;">DECLARE</span> @firstWeekDayInRange datetime<span style="color: #66cc66;">,</span> @lastWeekDayInRange datetime;
<span style="color: #993333; font-weight: bold;">SELECT</span> @firstWeekDayInRange <span style="color: #66cc66;">=</span>
<span style="color: #993333; font-weight: bold;">CASE</span>
<span style="color: #808080; font-style: italic;">-- If Saturday, add two days</span>
<span style="color: #993333; font-weight: bold;">WHEN</span> datepart<span style="color: #66cc66;">&#40;</span>dw<span style="color: #66cc66;">,</span>@<span style="color: #993333; font-weight: bold;">START</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">7</span> <span style="color: #993333; font-weight: bold;">THEN</span> dateadd<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span>@<span style="color: #993333; font-weight: bold;">START</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">-- If Sunday, add one day</span>
<span style="color: #993333; font-weight: bold;">WHEN</span> datepart<span style="color: #66cc66;">&#40;</span>dw<span style="color: #66cc66;">,</span>@<span style="color: #993333; font-weight: bold;">START</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">THEN</span> dateadd<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span>@<span style="color: #993333; font-weight: bold;">START</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">ELSE</span> @<span style="color: #993333; font-weight: bold;">START</span>
<span style="color: #993333; font-weight: bold;">END</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> @lastWeekDayInRange <span style="color: #66cc66;">=</span>
<span style="color: #993333; font-weight: bold;">CASE</span>
<span style="color: #808080; font-style: italic;">-- If Saturday, substract one day</span>
<span style="color: #993333; font-weight: bold;">WHEN</span> datepart<span style="color: #66cc66;">&#40;</span>dw<span style="color: #66cc66;">,</span>@<span style="color: #993333; font-weight: bold;">END</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">7</span> <span style="color: #993333; font-weight: bold;">THEN</span> dateadd<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span>@<span style="color: #993333; font-weight: bold;">END</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">-- If Sunday, substract two days</span>
<span style="color: #993333; font-weight: bold;">WHEN</span> datepart<span style="color: #66cc66;">&#40;</span>dw<span style="color: #66cc66;">,</span>@<span style="color: #993333; font-weight: bold;">END</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">THEN</span> dateadd<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,-</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span>@<span style="color: #993333; font-weight: bold;">END</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">ELSE</span> @<span style="color: #993333; font-weight: bold;">END</span>
<span style="color: #993333; font-weight: bold;">END</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- add one day to answer (to count Friday) if enddate was on a weekend</span>
<span style="color: #993333; font-weight: bold;">IF</span> @<span style="color: #993333; font-weight: bold;">END</span> !<span style="color: #66cc66;">=</span> @lastWeekDayInRange
<span style="color: #993333; font-weight: bold;">SET</span> @answer <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">SELECT</span> @answer <span style="color: #66cc66;">=</span> @answer <span style="color: #66cc66;">+</span>
<span style="color: #993333; font-weight: bold;">CASE</span>
<span style="color: #808080; font-style: italic;">-- triggered if start and end date are on same weekend</span>
<span style="color: #993333; font-weight: bold;">WHEN</span> dateDiff<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span>@firstWeekDayInRange<span style="color: #66cc66;">,</span>@lastWeekDayInRange<span style="color: #66cc66;">&#41;</span> &amp;lt; <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #66cc66;">&#40;</span>@answer <span style="color: #66cc66;">*</span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #808080; font-style: italic;">-- otherwise count the days and substract 2 days per weekend in between dates</span>
<span style="color: #993333; font-weight: bold;">ELSE</span> <span style="color: #66cc66;">&#40;</span>DateDiff<span style="color: #66cc66;">&#40;</span><span style="color: #993333; font-weight: bold;">DAY</span><span style="color: #66cc66;">,</span> @firstWeekDayInRange<span style="color: #66cc66;">,</span> @lastWeekDayInRange<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">-</span> DateDiff<span style="color: #66cc66;">&#40;</span>week<span style="color: #66cc66;">,</span> @firstWeekDayInRange<span style="color: #66cc66;">,</span> @lastWeekDayInRange<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">*</span><span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">END</span>
&nbsp;
<span style="color: #993333; font-weight: bold;">RETURN</span> @answer
<span style="color: #993333; font-weight: bold;">END</span>
&nbsp;
<span style="color: #808080; font-style: italic;">-- Call created function</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> dbo<span style="color: #66cc66;">.</span>dateDiffWeekdays<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'6/1/2009'</span><span style="color: #66cc66;">,</span><span style="color: #ff0000;">'6/13/2009'</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.aaronterry.com/2009/06/calculating-number-of-weekdays-between-two-dates/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

