<?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; Function to Get Previous And Next Work Day &#8211; Exclude Saturday and Sunday</title>
	<atom:link href="http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Fri, 17 May 2013 15:26:57 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Shantanu Agarwal</title>
		<link>http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-96252</link>
		<dc:creator><![CDATA[Shantanu Agarwal]]></dc:creator>
		<pubDate>Thu, 28 Oct 2010 08:05:39 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-96252</guid>
		<description><![CDATA[Hi,

 I need help in A stored procedure.
I have to calculate a parameter only on week days,
 For eg if i give input date to be 10th october(Sunday) and today is 28th october.

 I have to calculate these parameters for 5 weekdays..
So if i give A date which is a sunday then it should automatically skip that day and should calculate from Monday to friday.

Another eg. Suppose the input date is a wednesday, then calculate the parameters for wednesday, thursday, friday, moday and tuesday.

Skipping the Saturday and sunday. 

Could you please suggest a way out of this.]]></description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p> I need help in A stored procedure.<br />
I have to calculate a parameter only on week days,<br />
 For eg if i give input date to be 10th october(Sunday) and today is 28th october.</p>
<p> I have to calculate these parameters for 5 weekdays..<br />
So if i give A date which is a sunday then it should automatically skip that day and should calculate from Monday to friday.</p>
<p>Another eg. Suppose the input date is a wednesday, then calculate the parameters for wednesday, thursday, friday, moday and tuesday.</p>
<p>Skipping the Saturday and sunday. </p>
<p>Could you please suggest a way out of this.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adit</title>
		<link>http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-88927</link>
		<dc:creator><![CDATA[Adit]]></dc:creator>
		<pubDate>Wed, 22 Sep 2010 09:30:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-88927</guid>
		<description><![CDATA[it&#039;s usefull..
thanks for your code..

:D]]></description>
		<content:encoded><![CDATA[<p>it&#8217;s usefull..<br />
thanks for your code..</p>
<p>:D</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Idde</title>
		<link>http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-64160</link>
		<dc:creator><![CDATA[Idde]]></dc:creator>
		<pubDate>Wed, 31 Mar 2010 16:58:13 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-64160</guid>
		<description><![CDATA[Script to calculate hollidays (i.e not using a table... Tricky part is Easter :)


-- Drop function if exists
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE [id] = OBJECT_ID(N&#039;[dbo].[IsHolliday]&#039;) AND xtype IN (N&#039;FN&#039;, N&#039;IF&#039;, N&#039;TF&#039;)) DROP FUNCTION [dbo].[IsHolliday]
GO

-- Create function
CREATE FUNCTION dbo.IsHolliday
-- +--------------------------------------------------------------------------+
-- &#124; Function    : IsHolliday                                                 &#124;
-- &#124; Author      : Martin Grape                                               &#124;
-- &#124; Version     : 1.1                                                        &#124;
-- &#124; Description : Returns 1 for holliday and 0 for all other days            &#124;
-- &#124; Parameters  : @IsHolliday - Date to check                                &#124;
-- &#124; Returns     : 0 (workday) or 1 (holliday)                                &#124;
-- &#124; Other       : This is calculation for Swedish hollidays                  &#124;
-- &#124; Example     : SELECT dbo.IsHolliday(&#039;2010-04-04&#039;) --&gt; 1                  &#124;
-- +--------------------------------------------------------------------------+
( 
   @IsHolliday DATETIME
)
RETURNS INT
AS
BEGIN
   -- Declare variables
   DECLARE @IsHollidayResult INT
   DECLARE @IsHollidayYear INT
   DECLARE @IsHollidayMonth INT
   DECLARE @IsHollidayDay INT
   DECLARE @IsHollidayMonthDay VARCHAR(5)
   DECLARE @IsHollidayEasterDate DATETIME

   -- Declare variables
   SET @IsHollidayYear = YEAR(@IsHolliday)
   SET @IsHollidayMonth = MONTH(@IsHolliday)
   SET @IsHollidayDay = DAY(@IsHolliday)
   SET @IsHollidayMonthDay = RIGHT(CONVERT(CHAR(10), @IsHolliday, 120), 5)
    
   -- Set starting values
   SET @IsHollidayResult = 0

   -- Check for set dates (this is for Sweden)
   SET @IsHollidayResult = CASE WHEN @IsHollidayMonthDay IN (&#039;01-01&#039;, &#039;01-06&#039;, &#039;05-01&#039;, &#039;12-25&#039;, &#039;12-26&#039;) THEN 1 ELSE @IsHollidayResult END
   SET @IsHollidayResult = CASE WHEN @IsHollidayResult = 0 AND @IsHollidayMonthDay = &#039;06-06&#039; AND @IsHollidayYear &gt; 2004 THEN 1 ELSE @IsHollidayResult END

   -- Return if a set date
   IF @IsHollidayResult = 1 BEGIN
      RETURN @IsHollidayResult
   END

   -- Calculate easter date
   SET @IsHollidayEasterDate = CONVERT(DATETIME, CONVERT(CHAR(4), @IsHollidayYear) + &#039;-&#039; + 
                               RIGHT(&#039;00&#039; + CONVERT(VARCHAR, CONVERT(INT, ((((19 * (@IsHollidayYear % 19) + (CONVERT(INT, @IsHollidayYear / 100)) - (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) / 4)) - (CONVERT(INT, ((CONVERT(INT, @IsHollidayYear / 100)) - (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) + 8) / 25) + 1) / 3)) + 15) % 30)) + (((32 + 2 * ((CONVERT(INT, @IsHollidayYear / 100)) % 4) + 2 * (CONVERT(INT, (@IsHollidayYear % 100) / 4)) - (((19 * (@IsHollidayYear % 19) + (CONVERT(INT, @IsHollidayYear / 100)) - (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) / 4)) - (CONVERT(INT, ((CONVERT(INT, @IsHollidayYear / 100)) - (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) + 8) / 25) + 1) / 3)) + 15) % 30)) - ((@IsHollidayYear % 100) % 4)) % 7)) - 7 * (CONVERT(INT, ((@IsHollidayYear % 19) + 11 * (((19 * (@IsHollidayYear % 19) + (CONVERT(INT, @IsHollidayYear / 100)) - (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) / 4)) - (CONVERT(INT, ((CONVERT(INT, @IsHollidayYear / 100)) - (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) + 8) / 25) + 1) / 3)) + 15) % 30)) + 22 * (((32 + 2 * ((CONVERT(INT, @IsHollidayYear / 100)) % 4) + 2 * (CONVERT(INT, (@IsHollidayYear % 100) / 4)) - (((19 * (@IsHollidayYear % 19) + (CONVERT(INT, @IsHollidayYear / 100)) - (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) / 4)) - (CONVERT(INT, ((CONVERT(INT, @IsHollidayYear / 100)) - (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) + 8) / 25) + 1) / 3)) + 15) % 30)) - ((@IsHollidayYear % 100) % 4)) % 7))) / 451)) + 114) / 31)), 2) + &#039;-&#039; + 
                               RIGHT(&#039;00&#039; + CONVERT(VARCHAR, (((((19 * (@IsHollidayYear % 19) + (CONVERT(INT, @IsHollidayYear / 100)) - (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) / 4)) - (CONVERT(INT, ((CONVERT(INT, @IsHollidayYear / 100)) - (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) + 8) / 25) + 1) / 3)) + 15) % 30)) + (((32 + 2 * ((CONVERT(INT, @IsHollidayYear / 100)) % 4) + 2 * (CONVERT(INT, (@IsHollidayYear % 100) / 4)) - (((19 * (@IsHollidayYear % 19) + (CONVERT(INT, @IsHollidayYear / 100)) - (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) / 4)) - (CONVERT(INT, ((CONVERT(INT, @IsHollidayYear / 100)) - (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) + 8) / 25) + 1) / 3)) + 15) % 30)) - ((@IsHollidayYear % 100) % 4)) % 7)) - 7 * (CONVERT(INT, ((@IsHollidayYear % 19) + 11 * (((19 * (@IsHollidayYear % 19) + (CONVERT(INT, @IsHollidayYear / 100)) - (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) / 4)) - (CONVERT(INT, ((CONVERT(INT, @IsHollidayYear / 100)) - (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) + 8) / 25) + 1) / 3)) + 15) % 30)) + 22 * (((32 + 2 * ((CONVERT(INT, @IsHollidayYear / 100)) % 4) + 2 * (CONVERT(INT, (@IsHollidayYear % 100) / 4)) - (((19 * (@IsHollidayYear % 19) + (CONVERT(INT, @IsHollidayYear / 100)) - (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) / 4)) - (CONVERT(INT, ((CONVERT(INT, @IsHollidayYear / 100)) - (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) + 8) / 25) + 1) / 3)) + 15) % 30)) - ((@IsHollidayYear % 100) % 4)) % 7))) / 451)) + 114) % 31) + 1), 2))

   -- Easter
   IF @IsHolliday = @IsHollidayEasterDate - 2 OR
      @IsHolliday = @IsHollidayEasterDate OR
      @IsHolliday = @IsHollidayEasterDate + 1 BEGIN
      RETURN 1
   END

   -- Ascension Day
   IF @IsHolliday = DATEADD(dd, 39, @IsHollidayEasterDate) BEGIN
      RETURN 1 
   END

   -- Whitsun
   IF @IsHolliday = DATEADD(dd, 49, @IsHollidayEasterDate) OR 
      (@IsHolliday = DATEADD(dd, 50, @IsHollidayEasterDate) AND @IsHollidayYear &lt; 2005) BEGIN
      RETURN 1 
   END
 
   -- Midsummer
   IF @IsHolliday = DATEADD(dd, 7 - DATEPART(dw, CONVERT(CHAR(4), @IsHollidayYear) + &#039;-06-20&#039;), CONVERT(CHAR(4), @IsHollidayYear) + &#039;-06-20&#039;) BEGIN
      RETURN 1 
   END
   
   -- Halloween
   IF @IsHolliday = DATEADD(dd, 7 - DATEPART(dw, CONVERT(CHAR(4), @IsHollidayYear) + &#039;-10-31&#039;), CONVERT(CHAR(4), @IsHollidayYear) + &#039;-10-31&#039;) BEGIN
      RETURN 1 
   END

   RETURN 0
END 
GO]]></description>
		<content:encoded><![CDATA[<p>Script to calculate hollidays (i.e not using a table&#8230; Tricky part is Easter :)</p>
<p>&#8211; Drop function if exists<br />
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE [id] = OBJECT_ID(N&#8217;[dbo].[IsHolliday]&#8216;) AND xtype IN (N&#8217;FN&#8217;, N&#8217;IF&#8217;, N&#8217;TF&#8217;)) DROP FUNCTION [dbo].[IsHolliday]<br />
GO</p>
<p>&#8211; Create function<br />
CREATE FUNCTION dbo.IsHolliday<br />
&#8211; +&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
&#8211; | Function    : IsHolliday                                                 |<br />
&#8211; | Author      : Martin Grape                                               |<br />
&#8211; | Version     : 1.1                                                        |<br />
&#8211; | Description : Returns 1 for holliday and 0 for all other days            |<br />
&#8211; | Parameters  : @IsHolliday &#8211; Date to check                                |<br />
&#8211; | Returns     : 0 (workday) or 1 (holliday)                                |<br />
&#8211; | Other       : This is calculation for Swedish hollidays                  |<br />
&#8211; | Example     : SELECT dbo.IsHolliday(&#8217;2010-04-04&#8242;) &#8211;&gt; 1                  |<br />
&#8211; +&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+<br />
(<br />
   @IsHolliday DATETIME<br />
)<br />
RETURNS INT<br />
AS<br />
BEGIN<br />
   &#8212; Declare variables<br />
   DECLARE @IsHollidayResult INT<br />
   DECLARE @IsHollidayYear INT<br />
   DECLARE @IsHollidayMonth INT<br />
   DECLARE @IsHollidayDay INT<br />
   DECLARE @IsHollidayMonthDay VARCHAR(5)<br />
   DECLARE @IsHollidayEasterDate DATETIME</p>
<p>   &#8212; Declare variables<br />
   SET @IsHollidayYear = YEAR(@IsHolliday)<br />
   SET @IsHollidayMonth = MONTH(@IsHolliday)<br />
   SET @IsHollidayDay = DAY(@IsHolliday)<br />
   SET @IsHollidayMonthDay = RIGHT(CONVERT(CHAR(10), @IsHolliday, 120), 5)</p>
<p>   &#8212; Set starting values<br />
   SET @IsHollidayResult = 0</p>
<p>   &#8212; Check for set dates (this is for Sweden)<br />
   SET @IsHollidayResult = CASE WHEN @IsHollidayMonthDay IN (&#8217;01-01&#8242;, &#8217;01-06&#8242;, &#8217;05-01&#8242;, &#8217;12-25&#8242;, &#8217;12-26&#8242;) THEN 1 ELSE @IsHollidayResult END<br />
   SET @IsHollidayResult = CASE WHEN @IsHollidayResult = 0 AND @IsHollidayMonthDay = &#8217;06-06&#8242; AND @IsHollidayYear &gt; 2004 THEN 1 ELSE @IsHollidayResult END</p>
<p>   &#8212; Return if a set date<br />
   IF @IsHollidayResult = 1 BEGIN<br />
      RETURN @IsHollidayResult<br />
   END</p>
<p>   &#8212; Calculate easter date<br />
   SET @IsHollidayEasterDate = CONVERT(DATETIME, CONVERT(CHAR(4), @IsHollidayYear) + &#8216;-&#8217; +<br />
                               RIGHT(&#8217;00&#8242; + CONVERT(VARCHAR, CONVERT(INT, ((((19 * (@IsHollidayYear % 19) + (CONVERT(INT, @IsHollidayYear / 100)) &#8211; (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) / 4)) &#8211; (CONVERT(INT, ((CONVERT(INT, @IsHollidayYear / 100)) &#8211; (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) + 8) / 25) + 1) / 3)) + 15) % 30)) + (((32 + 2 * ((CONVERT(INT, @IsHollidayYear / 100)) % 4) + 2 * (CONVERT(INT, (@IsHollidayYear % 100) / 4)) &#8211; (((19 * (@IsHollidayYear % 19) + (CONVERT(INT, @IsHollidayYear / 100)) &#8211; (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) / 4)) &#8211; (CONVERT(INT, ((CONVERT(INT, @IsHollidayYear / 100)) &#8211; (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) + 8) / 25) + 1) / 3)) + 15) % 30)) &#8211; ((@IsHollidayYear % 100) % 4)) % 7)) &#8211; 7 * (CONVERT(INT, ((@IsHollidayYear % 19) + 11 * (((19 * (@IsHollidayYear % 19) + (CONVERT(INT, @IsHollidayYear / 100)) &#8211; (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) / 4)) &#8211; (CONVERT(INT, ((CONVERT(INT, @IsHollidayYear / 100)) &#8211; (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) + 8) / 25) + 1) / 3)) + 15) % 30)) + 22 * (((32 + 2 * ((CONVERT(INT, @IsHollidayYear / 100)) % 4) + 2 * (CONVERT(INT, (@IsHollidayYear % 100) / 4)) &#8211; (((19 * (@IsHollidayYear % 19) + (CONVERT(INT, @IsHollidayYear / 100)) &#8211; (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) / 4)) &#8211; (CONVERT(INT, ((CONVERT(INT, @IsHollidayYear / 100)) &#8211; (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) + 8) / 25) + 1) / 3)) + 15) % 30)) &#8211; ((@IsHollidayYear % 100) % 4)) % 7))) / 451)) + 114) / 31)), 2) + &#8216;-&#8217; +<br />
                               RIGHT(&#8217;00&#8242; + CONVERT(VARCHAR, (((((19 * (@IsHollidayYear % 19) + (CONVERT(INT, @IsHollidayYear / 100)) &#8211; (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) / 4)) &#8211; (CONVERT(INT, ((CONVERT(INT, @IsHollidayYear / 100)) &#8211; (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) + 8) / 25) + 1) / 3)) + 15) % 30)) + (((32 + 2 * ((CONVERT(INT, @IsHollidayYear / 100)) % 4) + 2 * (CONVERT(INT, (@IsHollidayYear % 100) / 4)) &#8211; (((19 * (@IsHollidayYear % 19) + (CONVERT(INT, @IsHollidayYear / 100)) &#8211; (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) / 4)) &#8211; (CONVERT(INT, ((CONVERT(INT, @IsHollidayYear / 100)) &#8211; (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) + 8) / 25) + 1) / 3)) + 15) % 30)) &#8211; ((@IsHollidayYear % 100) % 4)) % 7)) &#8211; 7 * (CONVERT(INT, ((@IsHollidayYear % 19) + 11 * (((19 * (@IsHollidayYear % 19) + (CONVERT(INT, @IsHollidayYear / 100)) &#8211; (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) / 4)) &#8211; (CONVERT(INT, ((CONVERT(INT, @IsHollidayYear / 100)) &#8211; (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) + 8) / 25) + 1) / 3)) + 15) % 30)) + 22 * (((32 + 2 * ((CONVERT(INT, @IsHollidayYear / 100)) % 4) + 2 * (CONVERT(INT, (@IsHollidayYear % 100) / 4)) &#8211; (((19 * (@IsHollidayYear % 19) + (CONVERT(INT, @IsHollidayYear / 100)) &#8211; (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) / 4)) &#8211; (CONVERT(INT, ((CONVERT(INT, @IsHollidayYear / 100)) &#8211; (CONVERT(INT, (CONVERT(INT, @IsHollidayYear / 100)) + 8) / 25) + 1) / 3)) + 15) % 30)) &#8211; ((@IsHollidayYear % 100) % 4)) % 7))) / 451)) + 114) % 31) + 1), 2))</p>
<p>   &#8212; Easter<br />
   IF @IsHolliday = @IsHollidayEasterDate &#8211; 2 OR<br />
      @IsHolliday = @IsHollidayEasterDate OR<br />
      @IsHolliday = @IsHollidayEasterDate + 1 BEGIN<br />
      RETURN 1<br />
   END</p>
<p>   &#8212; Ascension Day<br />
   IF @IsHolliday = DATEADD(dd, 39, @IsHollidayEasterDate) BEGIN<br />
      RETURN 1<br />
   END</p>
<p>   &#8212; Whitsun<br />
   IF @IsHolliday = DATEADD(dd, 49, @IsHollidayEasterDate) OR<br />
      (@IsHolliday = DATEADD(dd, 50, @IsHollidayEasterDate) AND @IsHollidayYear &lt; 2005) BEGIN<br />
      RETURN 1<br />
   END</p>
<p>   &#8212; Midsummer<br />
   IF @IsHolliday = DATEADD(dd, 7 &#8211; DATEPART(dw, CONVERT(CHAR(4), @IsHollidayYear) + &#039;-06-20&#039;), CONVERT(CHAR(4), @IsHollidayYear) + &#039;-06-20&#039;) BEGIN<br />
      RETURN 1<br />
   END</p>
<p>   &#8212; Halloween<br />
   IF @IsHolliday = DATEADD(dd, 7 &#8211; DATEPART(dw, CONVERT(CHAR(4), @IsHollidayYear) + &#039;-10-31&#039;), CONVERT(CHAR(4), @IsHollidayYear) + &#039;-10-31&#039;) BEGIN<br />
      RETURN 1<br />
   END</p>
<p>   RETURN 0<br />
END<br />
GO</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ellen</title>
		<link>http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-59116</link>
		<dc:creator><![CDATA[Ellen]]></dc:creator>
		<pubDate>Wed, 30 Dec 2009 01:32:06 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-59116</guid>
		<description><![CDATA[Built off this script to create the following udf that has option to exclude holidays and also to retrieve more than just 1 day prev/next:

CREATE FUNCTION dbo.udfGetPrevNextWorkDay 
(
	@StartDate DATETIME, 
	@bGetPrevious BIT = 0,
	@bExcludeHolidays BIT = 1,
	@MultipleDayCount INT = 0
)
RETURNS DATETIME  
AS
BEGIN

	DECLARE @EndDate DATETIME, @Day NVARCHAR(10), @DayCount INT 
	SET @Day = DATENAME(weekday, @StartDate)
	
	IF @MultipleDayCount &gt; 0
		BEGIN
			DECLARE @MultipleDayIndex INT, @MultipleDayDate DATETIME 
			SET @MultipleDayDate = @StartDate
			SET @MultipleDayIndex = 1
			
			WHILE @MultipleDayIndex &lt; @MultipleDayCount
				BEGIN
					SELECT @MultipleDayDate = dbo.udfGetPrevNextWorkDay(@MultipleDayDate, @bGetPrevious, @bExcludeHolidays, 0)
					SET @MultipleDayIndex = @MultipleDayIndex + 1
				END
			RETURN @MultipleDayDate
		END
		
	
	--To find Previous working day
	IF @bGetPrevious = 1
		BEGIN 
			SELECT @DayCount = 
				CASE @Day	WHEN &#039;Sunday&#039; THEN -2
							WHEN &#039;Monday&#039; THEN -3
							ELSE -1
				END 
		END
		
	--To find Next working day
	IF @bGetPrevious = 0
		BEGIN
			SELECT @DayCount = 
				CASE @Day	WHEN &#039;Friday&#039; THEN 3
							WHEN &#039;Saturday&#039; THEN 2
							ELSE 1
				END 
		END
	
	SET @EndDate = DATEADD(d,@DayCount,@StartDate)
	
	IF (@bExcludeHolidays = 1) AND (EXISTS(SELECT * FROM tblzcdIndexHoliday WHERE IndexHolidayCelebrationDate = @EndDate))
		BEGIN
			SELECT @EndDate = dbo.udfGetPrevNextWorkDay(@EndDate, @bGetPrevious, @bExcludeHolidays, 0)
		END
		
	
	
	
	RETURN ISNULL(@EndDate, @StartDate)
END]]></description>
		<content:encoded><![CDATA[<p>Built off this script to create the following udf that has option to exclude holidays and also to retrieve more than just 1 day prev/next:</p>
<p>CREATE FUNCTION dbo.udfGetPrevNextWorkDay<br />
(<br />
	@StartDate DATETIME,<br />
	@bGetPrevious BIT = 0,<br />
	@bExcludeHolidays BIT = 1,<br />
	@MultipleDayCount INT = 0<br />
)<br />
RETURNS DATETIME<br />
AS<br />
BEGIN</p>
<p>	DECLARE @EndDate DATETIME, @Day NVARCHAR(10), @DayCount INT<br />
	SET @Day = DATENAME(weekday, @StartDate)</p>
<p>	IF @MultipleDayCount &gt; 0<br />
		BEGIN<br />
			DECLARE @MultipleDayIndex INT, @MultipleDayDate DATETIME<br />
			SET @MultipleDayDate = @StartDate<br />
			SET @MultipleDayIndex = 1</p>
<p>			WHILE @MultipleDayIndex &lt; @MultipleDayCount<br />
				BEGIN<br />
					SELECT @MultipleDayDate = dbo.udfGetPrevNextWorkDay(@MultipleDayDate, @bGetPrevious, @bExcludeHolidays, 0)<br />
					SET @MultipleDayIndex = @MultipleDayIndex + 1<br />
				END<br />
			RETURN @MultipleDayDate<br />
		END</p>
<p>	&#8211;To find Previous working day<br />
	IF @bGetPrevious = 1<br />
		BEGIN<br />
			SELECT @DayCount =<br />
				CASE @Day	WHEN &#039;Sunday&#039; THEN -2<br />
							WHEN &#039;Monday&#039; THEN -3<br />
							ELSE -1<br />
				END<br />
		END</p>
<p>	&#8211;To find Next working day<br />
	IF @bGetPrevious = 0<br />
		BEGIN<br />
			SELECT @DayCount =<br />
				CASE @Day	WHEN &#039;Friday&#039; THEN 3<br />
							WHEN &#039;Saturday&#039; THEN 2<br />
							ELSE 1<br />
				END<br />
		END</p>
<p>	SET @EndDate = DATEADD(d,@DayCount,@StartDate)</p>
<p>	IF (@bExcludeHolidays = 1) AND (EXISTS(SELECT * FROM tblzcdIndexHoliday WHERE IndexHolidayCelebrationDate = @EndDate))<br />
		BEGIN<br />
			SELECT @EndDate = dbo.udfGetPrevNextWorkDay(@EndDate, @bGetPrevious, @bExcludeHolidays, 0)<br />
		END</p>
<p>	RETURN ISNULL(@EndDate, @StartDate)<br />
END</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ellen</title>
		<link>http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-59111</link>
		<dc:creator><![CDATA[Ellen]]></dc:creator>
		<pubDate>Tue, 29 Dec 2009 21:57:26 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-59111</guid>
		<description><![CDATA[Just came across the need to do this myself. I made the function just call itself again in the event that the previous/next date falls on a holiday.


IF EXISTS(SELECT * FROM tblHolidays WHERE HolidayDate = @EndDate)
		BEGIN
			SELECT @EndDate = dbo.udf_GetPrevNextWorkDay(@EndDate, @strPrevNext)
		END]]></description>
		<content:encoded><![CDATA[<p>Just came across the need to do this myself. I made the function just call itself again in the event that the previous/next date falls on a holiday.</p>
<p>IF EXISTS(SELECT * FROM tblHolidays WHERE HolidayDate = @EndDate)<br />
		BEGIN<br />
			SELECT @EndDate = dbo.udf_GetPrevNextWorkDay(@EndDate, @strPrevNext)<br />
		END</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: robert</title>
		<link>http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-52454</link>
		<dc:creator><![CDATA[robert]]></dc:creator>
		<pubDate>Wed, 27 May 2009 03:52:10 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-52454</guid>
		<description><![CDATA[how can i get the balance which the only given is the breakdown of payments, transaction amount and transaction date and i will use the transaction date as parameters.

for example(table form): this shoul be the output:

customer----trans_amt----payments---trans_date----balance
robert--------200--------------10----------1-1-09----------190
robert--------200--------------50----------1-5-09----------140
robert--------200--------------60----------1-7-09----------80

i want to get balance for every trans_date

please help me
thanks

robert]]></description>
		<content:encoded><![CDATA[<p>how can i get the balance which the only given is the breakdown of payments, transaction amount and transaction date and i will use the transaction date as parameters.</p>
<p>for example(table form): this shoul be the output:</p>
<p>customer&#8212;-trans_amt&#8212;-payments&#8212;trans_date&#8212;-balance<br />
robert&#8212;&#8212;&#8211;200&#8212;&#8212;&#8212;&#8212;&#8211;10&#8212;&#8212;&#8212;-1-1-09&#8212;&#8212;&#8212;-190<br />
robert&#8212;&#8212;&#8211;200&#8212;&#8212;&#8212;&#8212;&#8211;50&#8212;&#8212;&#8212;-1-5-09&#8212;&#8212;&#8212;-140<br />
robert&#8212;&#8212;&#8211;200&#8212;&#8212;&#8212;&#8212;&#8211;60&#8212;&#8212;&#8212;-1-7-09&#8212;&#8212;&#8212;-80</p>
<p>i want to get balance for every trans_date</p>
<p>please help me<br />
thanks</p>
<p>robert</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: robert</title>
		<link>http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-52453</link>
		<dc:creator><![CDATA[robert]]></dc:creator>
		<pubDate>Wed, 27 May 2009 03:50:01 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-52453</guid>
		<description><![CDATA[how can i get the balance which the only given is the breakdown of payments, transaction amount and transaction date and i will use the transaction date as parameters.

for example(table form): this shoul be the output:

customer     trans_amt      payments        trans_date   balance
robert             200                  10                 1-1-09          190
robert             200                  50                 1-5-09          140
robert             200                  60                 1-7-09            80

i want to get balance for every trans_date

please help me
thanks

robert]]></description>
		<content:encoded><![CDATA[<p>how can i get the balance which the only given is the breakdown of payments, transaction amount and transaction date and i will use the transaction date as parameters.</p>
<p>for example(table form): this shoul be the output:</p>
<p>customer     trans_amt      payments        trans_date   balance<br />
robert             200                  10                 1-1-09          190<br />
robert             200                  50                 1-5-09          140<br />
robert             200                  60                 1-7-09            80</p>
<p>i want to get balance for every trans_date</p>
<p>please help me<br />
thanks</p>
<p>robert</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: George</title>
		<link>http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-48924</link>
		<dc:creator><![CDATA[George]]></dc:creator>
		<pubDate>Fri, 13 Mar 2009 16:16:56 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-48924</guid>
		<description><![CDATA[select DATEDIFF ( day , &#039;02/13/2009&#039; , &#039;02/12/2009&#039; )
select DATEDIFF ( hour , &#039;02/13/2009&#039; , &#039;02/12/2009&#039; )]]></description>
		<content:encoded><![CDATA[<p>select DATEDIFF ( day , &#8217;02/13/2009&#8242; , &#8217;02/12/2009&#8242; )<br />
select DATEDIFF ( hour , &#8217;02/13/2009&#8242; , &#8217;02/12/2009&#8242; )</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: George</title>
		<link>http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-48923</link>
		<dc:creator><![CDATA[George]]></dc:creator>
		<pubDate>Fri, 13 Mar 2009 16:11:42 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-48923</guid>
		<description><![CDATA[Simple and good...!!!

Works well for me...]]></description>
		<content:encoded><![CDATA[<p>Simple and good&#8230;!!!</p>
<p>Works well for me&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SQLAuthority News - Best Articles on SQLAuthority.com Journey to SQL Authority with Pinal Dave</title>
		<link>http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-47233</link>
		<dc:creator><![CDATA[SQLAuthority News - Best Articles on SQLAuthority.com Journey to SQL Authority with Pinal Dave]]></dc:creator>
		<pubDate>Tue, 24 Feb 2009 12:09:58 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-47233</guid>
		<description><![CDATA[[...] SQL SERVER - UDF - Function to Get Previous And Next Work Day - Exclude Saturday and Sunday [...]]]></description>
		<content:encoded><![CDATA[<p>[...] SQL SERVER &#8211; UDF &#8211; Function to Get Previous And Next Work Day &#8211; Exclude Saturday and Sunday [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rajesh</title>
		<link>http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-47168</link>
		<dc:creator><![CDATA[Rajesh]]></dc:creator>
		<pubDate>Tue, 24 Feb 2009 10:16:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-47168</guid>
		<description><![CDATA[hi Pianl,

  I need a help from u.

I am having a field name issueDate datatype as smalldatetime.
I need this field and also the previous date 

ex:  Item id             ItemName      IssueDate
         1023                 XXXX           12/02/2009
         1023                 XXXX           13/02/2009


I need the answer is diff between the 13/02/2009(LastDate)  - 12/02/2009(previous date)]]></description>
		<content:encoded><![CDATA[<p>hi Pianl,</p>
<p>  I need a help from u.</p>
<p>I am having a field name issueDate datatype as smalldatetime.<br />
I need this field and also the previous date </p>
<p>ex:  Item id             ItemName      IssueDate<br />
         1023                 XXXX           12/02/2009<br />
         1023                 XXXX           13/02/2009</p>
<p>I need the answer is diff between the 13/02/2009(LastDate)  &#8211; 12/02/2009(previous date)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anthony SMith</title>
		<link>http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-44310</link>
		<dc:creator><![CDATA[Anthony SMith]]></dc:creator>
		<pubDate>Thu, 20 Nov 2008 14:51:13 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-44310</guid>
		<description><![CDATA[This is a great function and I really like how this function works . Is there any way to account for Holidays? I have seen something where one could enter Holidays in a table (eg - tblHolidays --&gt;brilliant naming huh?). When the workday falls on the date in the table, the function would then go onto the next working day.

How would I modify this Function to do this?]]></description>
		<content:encoded><![CDATA[<p>This is a great function and I really like how this function works . Is there any way to account for Holidays? I have seen something where one could enter Holidays in a table (eg &#8211; tblHolidays &#8211;&gt;brilliant naming huh?). When the workday falls on the date in the table, the function would then go onto the next working day.</p>
<p>How would I modify this Function to do this?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Imran Mohammed</title>
		<link>http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-40396</link>
		<dc:creator><![CDATA[Imran Mohammed]]></dc:creator>
		<pubDate>Thu, 17 Jul 2008 20:38:31 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-40396</guid>
		<description><![CDATA[@RAHUL

You mean this ??

select datename(dd,getdate()+8)
Result: 25

select datename (dw,getdate() +8)
Result : Friday --(if today is July 17, Thursday)]]></description>
		<content:encoded><![CDATA[<p>@RAHUL</p>
<p>You mean this ??</p>
<p>select datename(dd,getdate()+8)<br />
Result: 25</p>
<p>select datename (dw,getdate() +8)<br />
Result : Friday &#8211;(if today is July 17, Thursday)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rahul</title>
		<link>http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-40379</link>
		<dc:creator><![CDATA[Rahul]]></dc:creator>
		<pubDate>Thu, 17 Jul 2008 13:15:09 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-40379</guid>
		<description><![CDATA[Hi,

I am looking for a function through which I can calculate a particular working day. let say I want to know the 8th working day from todays date.

Any help??? its urgent..

Thanks in advance.

Rahul]]></description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I am looking for a function through which I can calculate a particular working day. let say I want to know the 8th working day from todays date.</p>
<p>Any help??? its urgent..</p>
<p>Thanks in advance.</p>
<p>Rahul</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ying</title>
		<link>http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-34036</link>
		<dc:creator><![CDATA[Ying]]></dc:creator>
		<pubDate>Wed, 27 Feb 2008 21:03:10 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-34036</guid>
		<description><![CDATA[I am a newbie in SQL
I am trying to structure a query wherein 
I need to display data where the Qtrly end dates(3/31,6/30/9/30/12/31) have been moved for vendors.
also The Table has multiple records for the same vedor ending in 3/31 and I need to take a max

When I tried using date diff between Quarters for different Years it does not work.. any help here is appreciated]]></description>
		<content:encoded><![CDATA[<p>I am a newbie in SQL<br />
I am trying to structure a query wherein<br />
I need to display data where the Qtrly end dates(3/31,6/30/9/30/12/31) have been moved for vendors.<br />
also The Table has multiple records for the same vedor ending in 3/31 and I need to take a max</p>
<p>When I tried using date diff between Quarters for different Years it does not work.. any help here is appreciated</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Naomi</title>
		<link>http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-33660</link>
		<dc:creator><![CDATA[Naomi]]></dc:creator>
		<pubDate>Mon, 11 Feb 2008 08:10:57 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-33660</guid>
		<description><![CDATA[Try it:
select DATENAME(dw ,convert(datetime, substring(eventstartdttm, 0,9),3))
-- 3 for format dd/MM/yy

Good-Luck!]]></description>
		<content:encoded><![CDATA[<p>Try it:<br />
select DATENAME(dw ,convert(datetime, substring(eventstartdttm, 0,9),3))<br />
&#8211; 3 for format dd/MM/yy</p>
<p>Good-Luck!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chitra</title>
		<link>http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-33424</link>
		<dc:creator><![CDATA[Chitra]]></dc:creator>
		<pubDate>Fri, 01 Feb 2008 22:25:05 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/07/23/sql-server-udf-function-to-get-previous-and-next-work-day-exclude-saturday-and-sunday/#comment-33424</guid>
		<description><![CDATA[Hi Pianl,
   Hello, how are you? I need help with a sql query that I am trying to write. I am trying to convert a date into a day in a query where a field called  eventstartdttm is a varchar field that holds date and time

for example if substring(eventstartdttm, 0,9) returns 30th Jan 2008 then I need it to say Wednesday. Is there a function that can help me do that? Thanks

Warm Regards,
Chitra]]></description>
		<content:encoded><![CDATA[<p>Hi Pianl,<br />
   Hello, how are you? I need help with a sql query that I am trying to write. I am trying to convert a date into a day in a query where a field called  eventstartdttm is a varchar field that holds date and time</p>
<p>for example if substring(eventstartdttm, 0,9) returns 30th Jan 2008 then I need it to say Wednesday. Is there a function that can help me do that? Thanks</p>
<p>Warm Regards,<br />
Chitra</p>
]]></content:encoded>
	</item>
</channel>
</rss>
