<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; UDF &#8211; User Defined Function to Find Weekdays Between Two Dates</title>
	<atom:link href="http://blog.sqlauthority.com/2007/05/05/sql-server-udf-user-defined-function-to-find-weekdays-between-two-dates/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2007/05/05/sql-server-udf-user-defined-function-to-find-weekdays-between-two-dates/</link>
	<description>Notes of a SQL Server MVP and Database Administrator</description>
	<lastBuildDate>Sat, 21 Nov 2009 05:54:09 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: HM</title>
		<link>http://blog.sqlauthority.com/2007/05/05/sql-server-udf-user-defined-function-to-find-weekdays-between-two-dates/#comment-44255</link>
		<dc:creator>HM</dc:creator>
		<pubDate>Mon, 17 Nov 2008 21:05:45 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/05/05/sql-function-to-find-weekdays-between-two-dates/#comment-44255</guid>
		<description>I would like to search for DataLength(AcctNo) = &#039;2&#039; and if first position is = &#039;0&#039; then make AcctNo = 10x where x is second position character of acctno.</description>
		<content:encoded><![CDATA[<p>I would like to search for DataLength(AcctNo) = &#8216;2&#8242; and if first position is = &#8216;0&#8242; then make AcctNo = 10x where x is second position character of acctno.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: red</title>
		<link>http://blog.sqlauthority.com/2007/05/05/sql-server-udf-user-defined-function-to-find-weekdays-between-two-dates/#comment-39056</link>
		<dc:creator>red</dc:creator>
		<pubDate>Fri, 06 Jun 2008 17:51:09 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/05/05/sql-function-to-find-weekdays-between-two-dates/#comment-39056</guid>
		<description>if you want to know weekdays fractional to the second ...

create function dbo.WeekDays(@StartDate datetime, @EndDate datetime)
returns float
as
begin
  return (
    (cast(datediff(s, @StartDate, @EndDate) as float) / 86400)
    -(2*DateDiff(ww,@StartDate,@EndDate))+
    case when datepart(dw, @StartDate)=7 then 1 else 0 end
  )
end</description>
		<content:encoded><![CDATA[<p>if you want to know weekdays fractional to the second &#8230;</p>
<p>create function dbo.WeekDays(@StartDate datetime, @EndDate datetime)<br />
returns float<br />
as<br />
begin<br />
  return (<br />
    (cast(datediff(s, @StartDate, @EndDate) as float) / 86400)<br />
    -(2*DateDiff(ww,@StartDate,@EndDate))+<br />
    case when datepart(dw, @StartDate)=7 then 1 else 0 end<br />
  )<br />
end</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Wieland</title>
		<link>http://blog.sqlauthority.com/2007/05/05/sql-server-udf-user-defined-function-to-find-weekdays-between-two-dates/#comment-35060</link>
		<dc:creator>Wieland</dc:creator>
		<pubDate>Mon, 14 Apr 2008 15:51:42 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/05/05/sql-function-to-find-weekdays-between-two-dates/#comment-35060</guid>
		<description>I think the missing sign should be a &#039;&gt;&#039;, not an &#039;=&#039;. 

i.e.
WHEN (@LastPart &gt; 0) THEN @LastPart - 1</description>
		<content:encoded><![CDATA[<p>I think the missing sign should be a &#8216;&gt;&#8217;, not an &#8216;=&#8217;. </p>
<p>i.e.<br />
WHEN (@LastPart &gt; 0) THEN @LastPart &#8211; 1</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shms</title>
		<link>http://blog.sqlauthority.com/2007/05/05/sql-server-udf-user-defined-function-to-find-weekdays-between-two-dates/#comment-34271</link>
		<dc:creator>shms</dc:creator>
		<pubDate>Wed, 12 Mar 2008 03:42:18 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/05/05/sql-function-to-find-weekdays-between-two-dates/#comment-34271</guid>
		<description>how do i taking holidays when calculating number of working days. because i already create table holiday for store all weekend holidays and public holiday.</description>
		<content:encoded><![CDATA[<p>how do i taking holidays when calculating number of working days. because i already create table holiday for store all weekend holidays and public holiday.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://blog.sqlauthority.com/2007/05/05/sql-server-udf-user-defined-function-to-find-weekdays-between-two-dates/#comment-20005</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Thu, 15 Nov 2007 12:25:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/05/05/sql-function-to-find-weekdays-between-two-dates/#comment-20005</guid>
		<description>I did one like this:

CREATE FUNCTION [dbo].[WeekdaysCount] 
( @StartDate datetime,
@EndDate datetime )
RETURNS INT
AS
BEGIN

DECLARE @TotalDays int, @WorkDays int, @Sats int, @Suns int

IF (DATEDIFF(day, @StartDate, @EndDate) = 0)
BEGIN
RETURN ( 0 )
END

SET @TotalDays = DATEDIFF(day, @StartDate, @EndDate)
SELECT @Sats = DATEDIFF(ww,@StartDate,@EndDate) + CASE WHEN DATENAME(dw,@StartDate) = &#039;sunday&#039; THEN 1 ELSE 0 END,
@Suns = DATEDIFF(ww,@StartDate,@EndDate) + CASE WHEN DATENAME(dw,@EndDate) = &#039;saturday&#039; THEN 1 ELSE 0 END

SET @WorkDays = @TotalDays - @Sats - @Suns

RETURN ( @WorkDays )
END
GO</description>
		<content:encoded><![CDATA[<p>I did one like this:</p>
<p>CREATE FUNCTION [dbo].[WeekdaysCount]<br />
( @StartDate datetime,<br />
@EndDate datetime )<br />
RETURNS INT<br />
AS<br />
BEGIN</p>
<p>DECLARE @TotalDays int, @WorkDays int, @Sats int, @Suns int</p>
<p>IF (DATEDIFF(day, @StartDate, @EndDate) = 0)<br />
BEGIN<br />
RETURN ( 0 )<br />
END</p>
<p>SET @TotalDays = DATEDIFF(day, @StartDate, @EndDate)<br />
SELECT @Sats = DATEDIFF(ww,@StartDate,@EndDate) + CASE WHEN DATENAME(dw,@StartDate) = &#8217;sunday&#8217; THEN 1 ELSE 0 END,<br />
@Suns = DATEDIFF(ww,@StartDate,@EndDate) + CASE WHEN DATENAME(dw,@EndDate) = &#8217;saturday&#8217; THEN 1 ELSE 0 END</p>
<p>SET @WorkDays = @TotalDays &#8211; @Sats &#8211; @Suns</p>
<p>RETURN ( @WorkDays )<br />
END<br />
GO</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Clinton</title>
		<link>http://blog.sqlauthority.com/2007/05/05/sql-server-udf-user-defined-function-to-find-weekdays-between-two-dates/#comment-7779</link>
		<dc:creator>Clinton</dc:creator>
		<pubDate>Wed, 15 Aug 2007 07:26:30 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/05/05/sql-function-to-find-weekdays-between-two-dates/#comment-7779</guid>
		<description>Hi.  I get the following message when attempting to add your function.  Could you please confirm this works for you

Msg 102, Level 15, State 1, Procedure GetWorkingDays, Line 18
Incorrect syntax near &#039;@FirstPart&#039;.
Msg 102, Level 15, State 1, Procedure GetWorkingDays, Line 51
Incorrect syntax near &#039;END&#039;.</description>
		<content:encoded><![CDATA[<p>Hi.  I get the following message when attempting to add your function.  Could you please confirm this works for you</p>
<p>Msg 102, Level 15, State 1, Procedure GetWorkingDays, Line 18<br />
Incorrect syntax near &#8216;@FirstPart&#8217;.<br />
Msg 102, Level 15, State 1, Procedure GetWorkingDays, Line 51<br />
Incorrect syntax near &#8216;END&#8217;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://blog.sqlauthority.com/2007/05/05/sql-server-udf-user-defined-function-to-find-weekdays-between-two-dates/#comment-7607</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Mon, 13 Aug 2007 19:34:56 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/05/05/sql-function-to-find-weekdays-between-two-dates/#comment-7607</guid>
		<description>Missing &quot;=&quot; from line
WHEN (@LastPart 0) THEN @LastPart - 1
Should be
WHEN (@LastPart = 0) THEN @LastPart - 1</description>
		<content:encoded><![CDATA[<p>Missing &#8220;=&#8221; from line<br />
WHEN (@LastPart 0) THEN @LastPart &#8211; 1<br />
Should be<br />
WHEN (@LastPart = 0) THEN @LastPart &#8211; 1</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mike</title>
		<link>http://blog.sqlauthority.com/2007/05/05/sql-server-udf-user-defined-function-to-find-weekdays-between-two-dates/#comment-5886</link>
		<dc:creator>mike</dc:creator>
		<pubDate>Thu, 26 Jul 2007 02:32:20 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/05/05/sql-function-to-find-weekdays-between-two-dates/#comment-5886</guid>
		<description>also if you want this to be inclusive, add this right below the begin statement and before the first declare

select @StartDate = @StartDate -1
select @EndDate = @EndDate + 1</description>
		<content:encoded><![CDATA[<p>also if you want this to be inclusive, add this right below the begin statement and before the first declare</p>
<p>select @StartDate = @StartDate -1<br />
select @EndDate = @EndDate + 1</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mike</title>
		<link>http://blog.sqlauthority.com/2007/05/05/sql-server-udf-user-defined-function-to-find-weekdays-between-two-dates/#comment-5885</link>
		<dc:creator>mike</dc:creator>
		<pubDate>Thu, 26 Jul 2007 02:20:47 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/05/05/sql-function-to-find-weekdays-between-two-dates/#comment-5885</guid>
		<description>here is the complete text...


CREATE FUNCTION dbo.GetWorkingDays
  ( @StartDate datetime,
    @EndDate datetime )
RETURNS INT
AS
BEGIN
  DECLARE @WorkDays int, @FirstPart int
  DECLARE @FirstNum int, @TotalDays int
  DECLARE @LastNum int, @LastPart int
  IF (DATEDIFF(day, @StartDate, @EndDate) &lt; 2)
    BEGIN
      RETURN ( 0 )
    END
  SELECT
   @TotalDays = DATEDIFF(day, @StartDate, @EndDate) - 1,
   @FirstPart = CASE DATENAME(weekday, @StartDate)
                 WHEN &#039;Sunday&#039; THEN 6
                 WHEN &#039;Monday&#039; THEN 5
                 WHEN &#039;Tuesday&#039; THEN 4
                 WHEN &#039;Wednesday&#039; THEN 3
                 WHEN &#039;Thursday&#039; THEN 2
                 WHEN &#039;Friday&#039; THEN 1
                 WHEN &#039;Saturday&#039; THEN 0
               END,
   @FirstNum = CASE DATENAME(weekday, @StartDate)
                 WHEN &#039;Sunday&#039; THEN 5
                 WHEN &#039;Monday&#039; THEN 4
                 WHEN &#039;Tuesday&#039; THEN 3
                 WHEN &#039;Wednesday&#039; THEN 2
                 WHEN &#039;Thursday&#039; THEN 1
                 WHEN &#039;Friday&#039; THEN 0
                 WHEN &#039;Saturday&#039; THEN 0
               END
  IF (@TotalDays &lt; @FirstPart)
     BEGIN
       SELECT @WorkDays = @TotalDays
     END
  ELSE
     BEGIN
       SELECT @WorkDays = (@TotalDays - @FirstPart) / 7
       SELECT @LastPart = (@TotalDays - @FirstPart) % 7
       SELECT @LastNum = CASE
         WHEN (@LastPart  0) THEN @LastPart - 1
         ELSE 0
       END
       SELECT @WorkDays = @WorkDays * 5 + @FirstNum + @LastNum
     END
  RETURN ( @WorkDays )
END
GO</description>
		<content:encoded><![CDATA[<p>here is the complete text&#8230;</p>
<p>CREATE FUNCTION dbo.GetWorkingDays<br />
  ( @StartDate datetime,<br />
    @EndDate datetime )<br />
RETURNS INT<br />
AS<br />
BEGIN<br />
  DECLARE @WorkDays int, @FirstPart int<br />
  DECLARE @FirstNum int, @TotalDays int<br />
  DECLARE @LastNum int, @LastPart int<br />
  IF (DATEDIFF(day, @StartDate, @EndDate) &lt; 2)<br />
    BEGIN<br />
      RETURN ( 0 )<br />
    END<br />
  SELECT<br />
   @TotalDays = DATEDIFF(day, @StartDate, @EndDate) &#8211; 1,<br />
   @FirstPart = CASE DATENAME(weekday, @StartDate)<br />
                 WHEN &#8216;Sunday&#8217; THEN 6<br />
                 WHEN &#8216;Monday&#8217; THEN 5<br />
                 WHEN &#8216;Tuesday&#8217; THEN 4<br />
                 WHEN &#8216;Wednesday&#8217; THEN 3<br />
                 WHEN &#8216;Thursday&#8217; THEN 2<br />
                 WHEN &#8216;Friday&#8217; THEN 1<br />
                 WHEN &#8216;Saturday&#8217; THEN 0<br />
               END,<br />
   @FirstNum = CASE DATENAME(weekday, @StartDate)<br />
                 WHEN &#8216;Sunday&#8217; THEN 5<br />
                 WHEN &#8216;Monday&#8217; THEN 4<br />
                 WHEN &#8216;Tuesday&#8217; THEN 3<br />
                 WHEN &#8216;Wednesday&#8217; THEN 2<br />
                 WHEN &#8216;Thursday&#8217; THEN 1<br />
                 WHEN &#8216;Friday&#8217; THEN 0<br />
                 WHEN &#8216;Saturday&#8217; THEN 0<br />
               END<br />
  IF (@TotalDays &lt; @FirstPart)<br />
     BEGIN<br />
       SELECT @WorkDays = @TotalDays<br />
     END<br />
  ELSE<br />
     BEGIN<br />
       SELECT @WorkDays = (@TotalDays &#8211; @FirstPart) / 7<br />
       SELECT @LastPart = (@TotalDays &#8211; @FirstPart) % 7<br />
       SELECT @LastNum = CASE<br />
         WHEN (@LastPart  0) THEN @LastPart &#8211; 1<br />
         ELSE 0<br />
       END<br />
       SELECT @WorkDays = @WorkDays * 5 + @FirstNum + @LastNum<br />
     END<br />
  RETURN ( @WorkDays )<br />
END<br />
GO</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dal</title>
		<link>http://blog.sqlauthority.com/2007/05/05/sql-server-udf-user-defined-function-to-find-weekdays-between-two-dates/#comment-2033</link>
		<dc:creator>Dal</dc:creator>
		<pubDate>Sat, 05 May 2007 18:48:27 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/05/05/sql-function-to-find-weekdays-between-two-dates/#comment-2033</guid>
		<description>Alexander is good author. Thanks for bringing this to here.</description>
		<content:encoded><![CDATA[<p>Alexander is good author. Thanks for bringing this to here.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
