Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


IsDateAHoliday UDF **NEW**


IsDateAHoliday UDF **NEW**

Author
Message
Joe Colletti
Joe Colletti
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
Points: 2 Visits: 1
Comments posted to this topic are about the item IsDateAHoliday UDF **NEW**
steve folkerts
steve folkerts
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 163
A function to return holidays is a great idea. However, I used this function and found that Nov 24,2008 returns true (1) as a Holiday. November 24 is a Monday, and Thanksgiving is the 4th Thursday in November.

The TSQL can be simplified. My function is below (with some different holiday dates, change as needed). I set variables for the month, day of week, day of month and use those to determine whether the date is a holiday.
--==============================================================
CREATE FUNCTION [dbo].[udf_DateIsHoliday]
(
   @DateIn datetime
)
RETURNS bit
AS
BEGIN
DECLARE @mm tinyint;
DECLARE @dw tinyint;
DECLARE @dd tinyint;
SET @mm = DATEPART(mm,@DateIn);
SET @dw = DATEPART(dw,@DateIn);
SET @dd = DATEPART(dd,@DateIn);

-- January 1
IF(@mm = 1 and @dd = 1)
   RETURN 1;
   
--MemorialDay
-- Last Monday in May
IF(@mm = 5 and @dw = 2 and @dd >= 25)
   RETURN 1;

-- July 4
IF(@mm = 7 and @dd = 4)
   RETURN 1;

--Labor Day
-- first Monday in September
IF(@mm = 9 and @dw = 2 and @dd <= 7)
   RETURN 1;

-- Thanksgiving
-- 4th Thursday of Nov
IF(@mm = 11 and @dw = 5 and @dd >= 22 and @dd <= 28)
   RETURN 1;

-- Day After Thanksgiving
IF(@mm = 11 and @dw = 6 and @dd >= 23 and @dd <= 29)
   RETURN 1;

-- DEC 24
IF(@mm = 12 and @dd = 24)
   RETURN 1;

-- DEC 25
IF(@mm = 12 and @dd = 25)
   RETURN 1;

RETURN 0;
END
--==============================================================
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search