SQL Code Description: This user defined function is used to determine if a date is a holiday then 1 else 0.
/****** Object: UserDefinedFunction [dbo].[SA_holidayDates] Script Date: 05/23/2012 10:24:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[SA_holidayDates]
(
@Date datetime
)
RETURNS int
AS
/********************************************************************************************
Description: This function is used to determine if a date is a holiday then 1 else 0.
Holidays are Christmas, New Year's Day, July 4th, Memorial Day (last Monday of May) and Labor Day (first Monday of September)
dbo.SA_holidayDates
********************************************************************************************/
begin
Declare @IsHoliday int
Select @IsHoliday = 0
if @Date = (select CAST('12/25/' + convert(varchar(4),(datepart(yyyy,@Date))) as DATE))
or @Date = (select CAST('1/1/' + convert(varchar(4),(datepart(yyyy,@Date))) as DATE))
or @Date = (select CAST('7/4/' + convert(varchar(4),(datepart(yyyy,@Date))) as DATE))
or @Date = (select DATEADD(wk, DATEDIFF(wk,0,CAST('5/31/' + convert(varchar(4),(datepart(yyyy,@Date))) as DATE)), 0))
or @Date = (select DATEADD(wk, DATEDIFF(wk,0,CAST('9/1/' + convert(varchar(4),(datepart(yyyy,@Date))) as DATE))+1, 0) )
Set @IsHoliday = 1
--select @IsHoliday
Return @IsHoliday
End
GO
To test your data - use this testing query -
--testing dates for holidays
declare @StartDate datetime
--set @StartDate = '5/28/2012'
--set @StartDate = '9/3/2012'
--set @StartDate = '1/1/2012'
--set @StartDate = '12/25/2012'
--set @StartDate = '7/4/2012'
set @StartDate = '5/30/2011'
--set @StartDate = '9/5/2011'
--set @StartDate = '1/1/2011'
--set @StartDate = '12/25/2011'
--set @StartDate = '7/4/2011'
select dbo.SA_holidayDates(@StartDate)
--compliments of Ann Roth
No comments:
Post a Comment