-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoadHolidays.sql
66 lines (65 loc) · 2.56 KB
/
LoadHolidays.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
;WITH x AS
(
SELECT
TheDate,
TheFirstOfYear,
TheDayOfWeekInMonth,
TheMonth,
TheDayName,
TheDay,
TheLastDayOfWeekInMonth = ROW_NUMBER() OVER
(
PARTITION BY TheFirstOfMonth, TheDayOfWeek
ORDER BY TheDate DESC
)
FROM BI.DateDimension
),
s AS
(
SELECT TheDate, HolidayText = CASE
WHEN (TheDate = TheFirstOfYear)
THEN 'New Year''s Day'
WHEN (TheDayOfWeekInMonth = 3 AND TheMonth = 1 AND TheDayName = 'Monday')
THEN 'Martin Luther King Day' -- (3rd Monday in January)
WHEN (TheDayOfWeekInMonth = 3 AND TheMonth = 2 AND TheDayName = 'Monday')
THEN 'President''s Day' -- (3rd Monday in February)
WHEN (TheLastDayOfWeekInMonth = 1 AND TheMonth = 5 AND TheDayName = 'Monday')
THEN 'Memorial Day' -- (last Monday in May)
WHEN (TheMonth = 7 AND TheDay = 4)
THEN 'Independence Day' -- (July 4th)
WHEN (TheDayOfWeekInMonth = 1 AND TheMonth = 9 AND TheDayName = 'Monday')
THEN 'Labour Day' -- (first Monday in September)
WHEN (TheDayOfWeekInMonth = 2 AND TheMonth = 10 AND TheDayName = 'Monday')
THEN 'Columbus Day' -- Columbus Day (second Monday in October)
WHEN (TheMonth = 11 AND TheDay = 11)
THEN 'Veterans'' Day' -- (November 11th)
WHEN (TheDayOfWeekInMonth = 4 AND TheMonth = 11 AND TheDayName = 'Thursday')
THEN 'Thanksgiving Day' -- (Thanksgiving Day ()fourth Thursday in November)
WHEN (TheMonth = 12 AND TheDay = 25)
THEN 'Christmas Day'
END
FROM x
WHERE
(TheDate = TheFirstOfYear)
OR (TheDayOfWeekInMonth = 3 AND TheMonth = 1 AND TheDayName = 'Monday')
OR (TheDayOfWeekInMonth = 3 AND TheMonth = 2 AND TheDayName = 'Monday')
OR (TheLastDayOfWeekInMonth = 1 AND TheMonth = 5 AND TheDayName = 'Monday')
OR (TheMonth = 7 AND TheDay = 4)
OR (TheDayOfWeekInMonth = 1 AND TheMonth = 9 AND TheDayName = 'Monday')
OR (TheDayOfWeekInMonth = 2 AND TheMonth = 10 AND TheDayName = 'Monday')
OR (TheMonth = 11 AND TheDay = 11)
OR (TheDayOfWeekInMonth = 4 AND TheMonth = 11 AND TheDayName = 'Thursday')
OR (TheMonth = 12 AND TheDay = 25)
)
INSERT INTO dbo.Special_Dates(ActualDate, ObservedDate,CommonName, IsHoliday, IsPaid)
SELECT TheDate
, CASE DATEPART(dw, TheDate) WHEN 7 THEN DATEADD(DAY, -1, TheDate)
WHEN 1 THEN DATEADD(DAY, 1, TheDate)
ELSE TheDate END ObservedDate
, HolidayText
, 1
, 1
FROM s
Where HolidayText NOT IN ( 'Martin Luther King Day', 'President''s Day', 'Columbus Day', 'Veterans'' Day' )
AND TheDate > ='2027-01-01'
ORDER BY TheDate;