-
Notifications
You must be signed in to change notification settings - Fork 0
/
udfDateTimes.sql
196 lines (186 loc) · 15.5 KB
/
udfDateTimes.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
IF OBJECT_ID('udfDateTimes') IS NOT NULL
DROP FUNCTION udfDateTimes
GO
-- function returns datetimes between the start and end date
-- broken down by the time increment
-- AUTHOR: Michael Allen Smith
-- REPO: https://github.com/digitalcolony/sql-server-outer-join-date-range
CREATE FUNCTION udfDateTimes (
@startDate DATETIME,
@endDate DATETIME,
@interval INT=1, -- this is the interval per daypart default is 1
@daypart VARCHAR(10)
)
RETURNS @DateTimeTable TABLE (
dtime DATETIME
)
AS
BEGIN
DECLARE @lenDiff TINYINT
DECLARE @dateDiff INT
SELECT @dateDiff = CASE @daypart
WHEN 'year' THEN DATEDIFF(yy,@startDate,@endDate)
WHEN 'quarter' THEN DATEDIFF(qq,@startDate,@endDate)
WHEN 'month' THEN DATEDIFF(mm,@startDate,@endDate)
WHEN 'week' THEN DATEDIFF(ww,@startDate,@endDate)
WHEN 'day' THEN DATEDIFF(dd,@startDate,@endDate)
WHEN 'hour' THEN DATEDIFF(hh,@startDate,@endDate)
WHEN 'minute' THEN DATEDIFF(mi,@startDate,@endDate) END
SET @lenDiff = LEN(@dateDiff)
-- Declare table with digits 0-9
DECLARE @digits TABLE (digit TINYINT)
INSERT INTO @digits
(digit)
SELECT 0
UNION
SELECT 1
UNION
SELECT 2
UNION
SELECT 3
UNION
SELECT 4
UNION
SELECT 5
UNION
SELECT 6
UNION
SELECT 7
UNION
SELECT 8
UNION
SELECT 9
IF @lenDiff < 3
INSERT INTO @DateTimeTable
SELECT CASE @daypart
WHEN 'year' THEN DATEADD(yy,10 * Tens.digit + Ones.digit,@startDate)
WHEN 'quarter' THEN DATEADD(qq,10 * Tens.digit + Ones.digit,@startDate)
WHEN 'month' THEN DATEADD(mm,10 * Tens.digit + Ones.digit,@startDate)
WHEN 'week' THEN DATEADD(ww,10 * Tens.digit + Ones.digit,@startDate)
WHEN 'day' THEN DATEADD(dd,10 * Tens.digit + Ones.digit,@startDate)
WHEN 'hour' THEN DATEADD(hh,10 * Tens.digit + Ones.digit,@startDate)
WHEN 'minute' THEN DATEADD(mi,10 * Tens.digit + Ones.digit,@startDate) END AS dtime
FROM @digits Tens CROSS JOIN @digits Ones
WHERE CASE @daypart
WHEN 'year' THEN DATEADD(yy,10 * Tens.digit + Ones.digit,@startDate)
WHEN 'quarter' THEN DATEADD(qq,10 * Tens.digit + Ones.digit,@startDate)
WHEN 'month' THEN DATEADD(mm,10 * Tens.digit + Ones.digit,@startDate)
WHEN 'week' THEN DATEADD(ww,10 * Tens.digit + Ones.digit,@startDate)
WHEN 'day' THEN DATEADD(dd,10 * Tens.digit + Ones.digit,@startDate)
WHEN 'hour' THEN DATEADD(hh,10 * Tens.digit + Ones.digit,@startDate)
WHEN 'minute' THEN DATEADD(mi,10 * Tens.digit + Ones.digit,@startDate) END
BETWEEN @startDate AND @endDate
AND (10 * Tens.digit + Ones.digit)%@interval = 0
ORDER BY 10 * Tens.digit + Ones.digit
IF @lenDiff = 3
INSERT INTO @DateTimeTable
SELECT CASE @daypart
WHEN 'year' THEN DATEADD(yy,100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'quarter' THEN DATEADD(qq,100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'month' THEN DATEADD(mm,100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'week' THEN DATEADD(ww,100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'day' THEN DATEADD(dd,100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'hour' THEN DATEADD(hh,100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'minute' THEN DATEADD(mi,100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate) END AS dtime
FROM @digits Hundreds CROSS JOIN @digits Tens CROSS JOIN @digits Ones
WHERE CASE @daypart
WHEN 'year' THEN DATEADD(yy,100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'quarter' THEN DATEADD(qq,100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'month' THEN DATEADD(mm,100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'week' THEN DATEADD(ww,100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'day' THEN DATEADD(dd,100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'hour' THEN DATEADD(hh,100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'minute' THEN DATEADD(mi,100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate) END
BETWEEN @startDate AND @endDate
AND (100 * Hundreds.digit + 10 * Tens.digit + Ones.digit)%@interval = 0
ORDER BY 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit
IF @lenDiff = 4
INSERT INTO @DateTimeTable
SELECT CASE @daypart
WHEN 'year' THEN DATEADD(yy,1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'quarter' THEN DATEADD(qq,1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'month' THEN DATEADD(mm,1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'week' THEN DATEADD(ww,1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'day' THEN DATEADD(dd,1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'hour' THEN DATEADD(hh,1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'minute' THEN DATEADD(mi,1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate) END AS dtime
FROM @digits Thousands CROSS JOIN @digits Hundreds CROSS JOIN @digits Tens CROSS JOIN @digits Ones
WHERE CASE @daypart
WHEN 'year' THEN DATEADD(yy,1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'quarter' THEN DATEADD(qq,1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'month' THEN DATEADD(mm,1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'week' THEN DATEADD(ww,1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'day' THEN DATEADD(dd,1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'hour' THEN DATEADD(hh,1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'minute' THEN DATEADD(mi,1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate) END
BETWEEN @startDate AND @endDate
AND (1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit)%@interval = 0
ORDER BY 1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit
IF @lenDiff = 5
INSERT INTO @DateTimeTable
SELECT CASE @daypart
WHEN 'year' THEN DATEADD(yy,10000 * TenThousands.digit + 1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'quarter' THEN DATEADD(qq,10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'month' THEN DATEADD(mm,10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'week' THEN DATEADD(ww,10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'day' THEN DATEADD(dd,10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'hour' THEN DATEADD(hh,10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'minute' THEN DATEADD(mi,10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate) END AS dtime
FROM @digits TenThousands CROSS JOIN @digits Thousands CROSS JOIN @digits Hundreds CROSS JOIN @digits Tens CROSS JOIN @digits Ones
WHERE CASE @daypart
WHEN 'year' THEN DATEADD(yy,10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'quarter' THEN DATEADD(qq,10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'month' THEN DATEADD(mm,10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'week' THEN DATEADD(ww,10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'day' THEN DATEADD(dd,10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'hour' THEN DATEADD(hh,10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'minute' THEN DATEADD(mi,10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate) END
BETWEEN @startDate AND @endDate
AND (10000 * TenThousands.digit + 1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit)%@interval = 0
ORDER BY 10000 * TenThousands.digit + 1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit
IF @lenDiff = 6
INSERT INTO @DateTimeTable
SELECT CASE @daypart
WHEN 'year' THEN DATEADD(yy,100000 * HundredThousands.digit + 10000 * TenThousands.digit + 1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'quarter' THEN DATEADD(qq,100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'month' THEN DATEADD(mm,100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'week' THEN DATEADD(ww,100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'day' THEN DATEADD(dd,100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'hour' THEN DATEADD(hh,100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'minute' THEN DATEADD(mi,100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate) END AS dtime
FROM @digits HundredThousands CROSS JOIN @digits TenThousands CROSS JOIN @digits Thousands CROSS JOIN @digits Hundreds CROSS JOIN @digits Tens CROSS JOIN @digits Ones
WHERE CASE @daypart
WHEN 'year' THEN DATEADD(yy,100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'quarter' THEN DATEADD(qq,100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'month' THEN DATEADD(mm,100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'week' THEN DATEADD(ww,100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'day' THEN DATEADD(dd,100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'hour' THEN DATEADD(hh,100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'minute' THEN DATEADD(mi,100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate) END
BETWEEN @startDate AND @endDate
AND (100000 * HundredThousands.digit + 10000 * TenThousands.digit + 1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit)%@interval = 0
ORDER BY 100000 * HundredThousands.digit + 10000 * TenThousands.digit + 1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit
IF @lenDiff = 7
INSERT INTO @DateTimeTable
SELECT CASE @daypart
WHEN 'year' THEN DATEADD(yy,1000000 * Millions.digit + 100000 * HundredThousands.digit + 10000 * TenThousands.digit + 1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'quarter' THEN DATEADD(qq,1000000 * Millions.digit + 100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'month' THEN DATEADD(mm,1000000 * Millions.digit + 100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'week' THEN DATEADD(ww,1000000 * Millions.digit + 100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'day' THEN DATEADD(dd,1000000 * Millions.digit + 100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'hour' THEN DATEADD(hh,1000000 * Millions.digit + 100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'minute' THEN DATEADD(mi,1000000 * Millions.digit + 100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate) END AS dtime
FROM @digits Millions CROSS JOIN @digits HundredThousands CROSS JOIN @digits TenThousands CROSS JOIN @digits Thousands CROSS JOIN @digits Hundreds CROSS JOIN @digits Tens CROSS JOIN @digits Ones
WHERE CASE @daypart
WHEN 'year' THEN DATEADD(yy,1000000 * Millions.digit + 100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'quarter' THEN DATEADD(qq,1000000 * Millions.digit + 100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'month' THEN DATEADD(mm,1000000 * Millions.digit + 100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'week' THEN DATEADD(ww,1000000 * Millions.digit + 100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'day' THEN DATEADD(dd,1000000 * Millions.digit + 100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'hour' THEN DATEADD(hh,1000000 * Millions.digit + 100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate)
WHEN 'minute' THEN DATEADD(mi,1000000 * Millions.digit + 100000 * HundredThousands.digit + 10000 * TenThousands.digit +1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit,@startDate) END
BETWEEN @startDate AND @endDate
AND (1000000 * Millions.digit + 100000 * HundredThousands.digit + 10000 * TenThousands.digit + 1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit)%@interval = 0
ORDER BY 1000000 * Millions.digit + 100000 * HundredThousands.digit + 10000 * TenThousands.digit + 1000 * Thousands.digit + 100 * Hundreds.digit + 10 * Tens.digit + Ones.digit
RETURN
END