SQL Script for Canadian Holidays
``USE [ExternalTables]
GO
/****** Object: UserDefinedFunction [dbo].[Holiday_List_Ontario] Script Date: 5/23/2018 10:35:53 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[Holiday_List_Ontario] (@nYear INT)
RETURNS @Holidays TABLE
(
Holiday_Name VARCHAR(32),
Holiday_Date SMALLDATETIME,
Holiday_Country VARCHAR(32),
Holiday_Province VARCHAR(32),
Holiday_Year INT
)
AS
BEGIN
-- Calculate Easter Sunday
DECLARE @g INT
DECLARE @c INT
DECLARE @h INT
DECLARE @i INT
DECLARE @j INT
DECLARE @l INT
DECLARE @Month INT
DECLARE @Day INT
DECLARE @Easter SMALLDATETIME
DECLARE @WorkDT SMALLDATETIME
SET @g = @nYear % 19
SET @c = @nYear / 100
SET @h = ((@c - (@c / 4) - ((8 * @c + 13) / 25) + (19 * @g) + 15) % 30)
SET @i = @h - ((@h / 28) * (1 - (@h /28) * (29 / (@h + 1)) * ((21 - @g) / 11)))
SET @j = ((@nYear + (@nYear / 4) + @i + 2 - @c + (@c / 4)) % 7)
SET @l = @i - @j
SET @Month = 3 + ((@l + 40) / 44)
SET @Day = @l + 28 - (31 * (@Month / 4))
SET @Easter = CAST(@Month AS VARCHAR(2)) + '/' + CAST(@Day As VARCHAR(2)) + '/' + CAST(@nYear AS VARCHAR(4))
------------------------------------------------------------------------------------------------
-- Add Easter Sunday to holiday list, and get holidays based around Easter
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Easter',@Easter,'Canada','Ontario',@nYear);
-- Good Friday
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Good Friday',DateAdd(d,-2,@Easter),'Canada','Ontario',@nYear);
-------------------------------------------------------------------------------------------------
-- Fixed date holidays are loaded next
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('New Year''s Day',CONVERT(Date,'1/1/'+CAST(@nYear AS VARCHAR(4))),'Canada','Ontario',@nYear)
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Halloween',CONVERT(SmallDateTime,'10/31/'+CAST(@nYear AS VARCHAR(4))),'Canada','Ontario',@nYear)
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Family Day',dbo.FloatingDate(3,2,2,@nYear),'Canada','Ontario',@nYear) -- 3rd Monday in February
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Labour Day',dbo.FloatingDate(1,2,9,@nYear),'Canada','Ontario',@nYear) -- 1rst Monday in September
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Thanksgiving',dbo.FloatingDate(2,2,10,@nYear),'Canada','Ontario',@nYear) -- 2nd Monday in October
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Civic Holiday',dbo.FloatingDate(1,2,8,@nYear),'Canada','Ontario',@nYear) -- 1st Monday in August
DECLARE @CanadaDayWD as int;
SELECT @CanadaDayWD = DATEPART(WEEKDAY, CAST(CAST(@nYear AS VARCHAR(4))+ '-07-01' as Date));
if(@CanadaDayWD = 1) -- Canada Day on Sunday
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Canada Day',CONVERT(Date,'7/2/'+CAST(@nYear AS VARCHAR(4))),'Canada','Ontario',@nYear)
if(@CanadaDayWD = 7) -- Canada Day on Saturday
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Canada Day',CONVERT(Date,'7/3/'+CAST(@nYear AS VARCHAR(4))),'Canada','Ontario',@nYear)
if(@CanadaDayWD = 2) OR (@CanadaDayWD = 3) OR (@CanadaDayWD = 4) OR (@CanadaDayWD = 5) OR (@CanadaDayWD = 6)
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Canada Day',CONVERT(Date,'7/1/'+CAST(@nYear AS VARCHAR(4))),'Canada','Ontario',@nYear)
DECLARE @ChristmasWD as int;
SELECT @ChristmasWD = DATEPART(WEEKDAY, CAST(CAST(@nYear AS VARCHAR(4))+ '-12-25' as Date));
if(@ChristmasWD = 1) -- Christmas on Sunday
BEGIN
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Christmas',CONVERT(Date,'12/26/'+CAST(@nYear AS VARCHAR(4))),'Canada','Ontario',@nYear)
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Boxing Day',CONVERT(Date,'12/27/'+CAST(@nYear AS VARCHAR(4))),'Canada','Ontario',@nYear)
END
if(@ChristmasWD = 7) -- Christmas on Saturday
BEGIN
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Christmas',CONVERT(Date,'12/27/'+CAST(@nYear AS VARCHAR(4))),'Canada','Ontario',@nYear)
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Boxing Day',CONVERT(Date,'12/28/'+CAST(@nYear AS VARCHAR(4))),'Canada','Ontario',@nYear)
END
if(@ChristmasWD = 6) -- Christmas on FRIDAY
BEGIN
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Christmas',CONVERT(Date,'12/24/'+CAST(@nYear AS VARCHAR(4))),'Canada','Ontario',@nYear)
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Boxing Day',CONVERT(Date,'12/28/'+CAST(@nYear AS VARCHAR(4))),'Canada','Ontario',@nYear)
END
if(@ChristmasWD = 2) OR (@ChristmasWD = 3) OR (@ChristmasWD = 4) OR (@ChristmasWD = 5) -- Christmas on Monday to Thursday
BEGIN
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Christmas',CONVERT(Date,'12/25/'+CAST(@nYear AS VARCHAR(4))),'Canada','Ontario',@nYear)
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Boxing Day',CONVERT(Date,'12/26/'+CAST(@nYear AS VARCHAR(4))),'Canada','Ontario',@nYear)
END
-- Victoria Day
DECLARE @VictoriaDay as date;
SELECT @VictoriaDay = (dbo.FloatingDate(5,2,5,@nYear));
if(@VictoriaDay < CAST(CAST(@nYear AS VARCHAR(4))+ '-05-24' as Date))
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Victoria Day',@VictoriaDay,'Canada','Ontario',@nYear);
Else
BEGIN
SELECT @VictoriaDay = (dbo.FloatingDate(4,2,5,@nYear));
if(@VictoriaDay < CAST(CAST(@nYear AS VARCHAR(4))+ '-05-24' as Date))
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Victoria Day',@VictoriaDay,'Canada','Ontario',@nYear);
Else
BEGIN
SELECT @VictoriaDay = (dbo.FloatingDate(3,2,5,@nYear));
if(@VictoriaDay < CAST(CAST(@nYear AS VARCHAR(4))+ '-05-24' as Date))
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Victoria Day',@VictoriaDay,'Canada','Ontario',@nYear);
Else
BEGIN
SELECT @VictoriaDay = (dbo.FloatingDate(3,2,5,@nYear));
INSERT INTO @Holidays (Holiday_name,Holiday_date,Holiday_Country,Holiday_Province,Holiday_Year) VALUES ('Victoria Day',@VictoriaDay,'Canada','Ontario',@nYear);
END
END
END
RETURN
END``