-
Notifications
You must be signed in to change notification settings - Fork 681
/
fn_recurringDates.sql
158 lines (128 loc) · 5.83 KB
/
fn_recurringDates.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
CREATE FUNCTION dbo.fn_recurringDates(
@wkDayPattern tinyint=127, --- 1=Mon, 2=Tue, 4=Wed, ... 127=All
@dayFrequency tinyint=1, --- 1=All, 2=every second, ...
@exactDay tinyint=NULL, --- Specific day number of the month
@occurrenceNo tinyint=NULL, -- 1=First, 2=Second, ... 0=Last
@occurrenceType tinyint=NULL, -- ... of 1=Week, 2=Month, 3=Year
@weekFrequency tinyint=1, --- 1=Every week, 2=Every second, etc
@exactWeek tinyint=NULL, -- Specific ISO week of the year
@monPattern smallint=4095, -- 1=Jan, 2=Feb, 4=March, ...
@monFrequency tinyint=1, --- 1=Every month, 2=Every second...
@yearFrequency tinyint=1, --- 1=Every year, 2=Every two...
@start date, --- Start date of recurrence
@end date=NULL, --- End date of recurrence
@occurrences int=NULL --- Max number of occurrences
)
RETURNS @dates TABLE (
[date] date NOT NULL,
PRIMARY KEY CLUSTERED ([date])
)
AS
BEGIN
--- Variable declarations:
DECLARE @occurrenceCount int=0, @year date=@start;
--- Make sure the parameters are set correctly:
IF (@occurrences IS NULL AND @end IS NULL) RETURN;
IF (@occurrenceNo IS NOT NULL AND @occurrenceType IS NULL)
SET @occurrenceNo=NULL;
--- This loop will start off with @year=@start and then
--- increase @year by one calendar year for every iteration:
WHILE (@occurrenceCount<@occurrences AND
DATEDIFF(yy, @start, @year)<@yearFrequency*@occurrences OR
@year<@end) BEGIN;
--- Build a recursive common table expression that loops
--- through every date from @year and one year forward.
WITH dates ([date], occurrence)
AS (
SELECT @year, 1
UNION ALL
SELECT DATEADD(dd, 1, [date]), occurrence+1
FROM dates
WHERE DATEADD(dd, 1, [date])<DATEADD(yy, 1, @year))
--- INSERT the result into the output table, @dates
INSERT INTO @dates ([date])
SELECT [date]
FROM (
SELECT [date],
--- The "ordinal number of the year"
DATEDIFF(yy, @start, @year) AS yearOrdinal,
--- The ordinal number of the week (first week,
--- second, third, ...) starting with @year.
DENSE_RANK() OVER (
ORDER BY DATEPART(yy, [date]),
NULLIF(DATEPART(isoww, [date]), 0)
) AS wkOrdinal,
--- Ordinal number of the month, as of @year.
DENSE_RANK() OVER (
ORDER BY DATEPART(yy, [date]), DATEPART(mm, [date])
) AS monOrdinal,
--- Ordinal number of the day, as of @year.
ROW_NUMBER() OVER (
PARTITION BY DATEPART(yy, [date])
ORDER BY [date]
) AS dayOrdinal,
--- Ordinal number of the day, per @occurenceType,
--- as of @year:
ROW_NUMBER() OVER (
PARTITION BY (CASE @occurrenceType
WHEN 1 THEN DATEPART(isoww, [date])
WHEN 2 THEN DATEPART(mm, [date])
END),
(CASE WHEN @occurrenceType IN (1, 3)
THEN DATEPART(yy, [date]) END)
ORDER BY [date]
) AS dateOrdinal,
--- dayOrdinal (descending). Used to calculate
--- LAST occurrence (@occurenceNo=0)
ROW_NUMBER() OVER (
PARTITION BY (CASE @occurrenceType
WHEN 1 THEN DATEPART(isoww, [date])
WHEN 2 THEN DATEPART(mm, [date])
END),
(CASE WHEN @occurrenceType IN (1, 3)
THEN DATEPART(yy, [date]) END)
ORDER BY [date] DESC
) AS dateOrdinalDesc
FROM dates
WHERE
--- Logical AND to filter specific weekdays:
POWER(2, (DATEPART(dw, [date])+@@DATEFIRST+5)%7)
& @wkDayPattern>0 AND
--- Logical AND to filter specific months:
POWER(2, DATEPART(mm, [date])-1)
& @monPattern>0 AND
--- Filter specific ISO week numbers:
(@exactWeek IS NULL OR
DATEPART(isoww, [date])=@exactWeek) AND
--- Filter specific days of the month:
(@exactDay IS NULL OR
DATEPART(dd, [date])=@exactDay)
) AS sub
WHERE
--- Modulo operator, to filter yearly frequencies:
sub.yearOrdinal%@yearFrequency=0 AND
--- Modulo operator, to filter monthly frequencies:
sub.monOrdinal%@monFrequency=0 AND
--- Modulo operator, to filter weekly frequencies:
sub.wkOrdinal%@weekFrequency=0 AND
--- Modulo operator, to filter daily frequencies:
sub.dateOrdinal%@dayFrequency=0 AND
--- Filter day ordinal:
(@occurrenceNo IS NULL OR
@occurrenceNo=sub.dateOrdinal OR
@occurrenceNo=0 AND sub.dateOrdinalDesc=1) AND
--- ... and finally, stop if we reach @end:
sub.[date]<=ISNULL(@end, sub.[date])
--- The default is 100, so we'll get an error if we don't
--- explicitly allow for more recursions:
OPTION (MAXRECURSION 366);
--- Add the number of dates that we've added to the
--- @dates table to our counter, @occurrenceCount.
--- Also, increase @year by one year.
SELECT
@occurrenceCount=@occurrenceCount+@@ROWCOUNT,
@year=DATEADD(yy, 1, @year);
END;
RETURN;
END;
GO