-
Notifications
You must be signed in to change notification settings - Fork 5
/
dbo.uf_DateRange.UserDefinedFunction.sql
64 lines (60 loc) · 3.42 KB
/
dbo.uf_DateRange.UserDefinedFunction.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
IF OBJECT_ID('dbo.uf_DateRange') IS NOT NULL DROP FUNCTION dbo.uf_DateRange
GO
-- =============================================
-- Author: Chad Baldwin
-- Create date: 2017-04-13
-- Description: Dates
-- =============================================
CREATE FUNCTION dbo.uf_DateRange (
@DateFrom DATETIME,
@DateTo DATETIME,
@Increment VARCHAR(20)
)
RETURNS @Return TABLE (
BeginDate DATETIME,
EndDate DATETIME
)
AS
BEGIN
IF @Increment NOT IN ('year','yy','yyyy','quarter','qq','q','month','mm','m','week','wk','ww','day','dd','d','hour','hh','minute','mi','n','second','ss','s') RETURN
DECLARE @TargetDate DATETIME = @DateTo,
@LoopLimit INT = 5000,
@Counter INT = 0
DECLARE @DateRange TABLE (BeginDate DATETIME, EndDate DATETIME)
WHILE (@Counter < @LoopLimit)
BEGIN
INSERT INTO @Return (BeginDate, EndDate)
SELECT BeginDate = CASE WHEN @Increment IN ('year' , 'yy', 'yyyy') THEN DATEADD(yy, DATEDIFF(yy, 0, @TargetDate), 0)
WHEN @Increment IN ('quarter', 'qq', 'q' ) THEN DATEADD(qq, DATEDIFF(qq, 0, @TargetDate), 0)
WHEN @Increment IN ('month' , 'mm', 'm' ) THEN DATEADD(mm, DATEDIFF(mm, 0, @TargetDate), 0)
WHEN @Increment IN ('week' , 'wk', 'ww' ) THEN DATEADD(ww, DATEDIFF(ww, 0, @TargetDate), 0)
WHEN @Increment IN ('day' , 'dd', 'd' ) THEN DATEADD(dd, DATEDIFF(dd, 0, @TargetDate), 0)
WHEN @Increment IN ('hour' , 'hh' ) THEN DATEADD(hh, DATEDIFF(hh, 0, @TargetDate), 0)
WHEN @Increment IN ('minute' , 'mi', 'n' ) THEN DATEADD(mi, DATEDIFF(mi, 0, @TargetDate), 0)
WHEN @Increment IN ('second' , 'ss', 's' ) THEN DATEADD(ss, DATEDIFF(ss, 0, @TargetDate), 0)
END
, EndDate = DATEADD(ms, -3,
CASE WHEN @Increment IN ('year' , 'yy', 'yyyy') THEN DATEADD(yy, DATEDIFF(yy, 0, @TargetDate) + 1, 0)
WHEN @Increment IN ('quarter', 'qq', 'q' ) THEN DATEADD(qq, DATEDIFF(qq, 0, @TargetDate) + 1, 0)
WHEN @Increment IN ('month' , 'mm', 'm' ) THEN DATEADD(mm, DATEDIFF(mm, 0, @TargetDate) + 1, 0)
WHEN @Increment IN ('week' , 'wk', 'ww' ) THEN DATEADD(ww, DATEDIFF(ww, 0, @TargetDate) + 1, 0)
WHEN @Increment IN ('day' , 'dd', 'd' ) THEN DATEADD(dd, DATEDIFF(dd, 0, @TargetDate) + 1, 0)
WHEN @Increment IN ('hour' , 'hh' ) THEN DATEADD(hh, DATEDIFF(hh, 0, @TargetDate) + 1, 0)
WHEN @Increment IN ('minute' , 'mi', 'n' ) THEN DATEADD(mi, DATEDIFF(mi, 0, @TargetDate) + 1, 0)
WHEN @Increment IN ('second' , 'ss', 's' ) THEN DATEADD(ss, DATEDIFF(ss, 0, @TargetDate) + 1, 0)
END)
SET @TargetDate = CASE WHEN @Increment IN ('year' , 'yy', 'yyyy') THEN DATEADD(yy, -1, @TargetDate)
WHEN @Increment IN ('quarter', 'qq', 'q' ) THEN DATEADD(qq, -1, @TargetDate)
WHEN @Increment IN ('month' , 'mm', 'm' ) THEN DATEADD(mm, -1, @TargetDate)
WHEN @Increment IN ('week' , 'wk', 'ww' ) THEN DATEADD(ww, -1, @TargetDate)
WHEN @Increment IN ('day' , 'dd', 'd' ) THEN DATEADD(dd, -1, @TargetDate)
WHEN @Increment IN ('hour' , 'hh' ) THEN DATEADD(hh, -1, @TargetDate)
WHEN @Increment IN ('minute' , 'mi', 'n' ) THEN DATEADD(mi, -1, @TargetDate)
WHEN @Increment IN ('second' , 'ss', 's' ) THEN DATEADD(ss, -1, @TargetDate)
END
IF @TargetDate <= @DateFrom BREAK
SET @Counter += 1
END
RETURN
END
GO