Skip to content

Commit 994bee3

Browse files
authored
Update dbo.uf_DateCalc.UserDefinedFunction.sql
1 parent 788907f commit 994bee3

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

dbo.uf_DateCalc.UserDefinedFunction.sql

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ BEGIN;
1919
, DiffWK = DATEDIFF(WEEK , 0, @SeedDate)
2020
, DiffMM = DATEDIFF(MONTH , 0, @SeedDate)
2121
, DiffQQ = DATEDIFF(QUARTER, 0, @SeedDate)
22-
, DiffYY = DATEDIFF(YEAR , 0, @SeedDate)
22+
, DiffYY = DATEDIFF(YEAR , 0, @SeedDate)
2323
)
2424
INSERT INTO @Return (Code, [Label], BeginDate, EndDateInclusiveDT, EndDateInclusiveDT2, EndDateExclusive)
2525
SELECT Code = x.Code
@@ -51,8 +51,8 @@ BEGIN;
5151
) x(Code, [Period], BeginDate, EndDate)
5252
WHERE x.Code = @DateCode OR @DateCode IS NULL
5353
UNION
54-
-- Handling for P7D and L7D style date codes
55-
-- Supporting only days for now. Months requires a bit more work to calcualte the end of month value.
54+
-- Handling for P7D and L7D style date codes
55+
-- Supporting only days for now. Months requires a bit more work to calcualte the end of month value.
5656
SELECT Code = UPPER(@DateCode)
5757
, [Label] = CONCAT_WS(' ', CASE LEFT(@DateCode, 1) WHEN 'L' THEN 'Last' WHEN 'P' THEN 'Previous' ELSE NULL END, t.[Value], 'days')
5858
, BeginDate = CONVERT(datetime2, x.BeginDate)
@@ -69,7 +69,18 @@ BEGIN;
6969
) x
7070
WHERE @DateCode LIKE '[LP][0-9]D'
7171
OR @DateCode LIKE '[LP][0-9][0-9]D'
72-
OR @DateCode LIKE '[LP][0-9][0-9][0-9]D';
72+
OR @DateCode LIKE '[LP][0-9][0-9][0-9]D'
73+
74+
IF (@DateCode IS NULL) -- If the DateCode is not provided, then fill in some standard options
75+
BEGIN;
76+
-- Ew, gross, recursion, I know. But, it's the easiest way to include both a common set of options, while still supporting dynamic entries
77+
INSERT INTO @Return (Code, [Label], BeginDate, EndDateInclusiveDT, EndDateInclusiveDT2, EndDateExclusive)
78+
SELECT y.Code, y.[Label], y.BeginDate, y.EndDateInclusiveDT, y.EndDateInclusiveDT2, y.EndDateExclusive
79+
FROM (VALUES ('L7D'),('L14D'),('L30D'),('L60D'),('L90D')
80+
,('P7D'),('P14D'),('P30D'),('P60D'),('P90D')
81+
) x(DateCode)
82+
CROSS APPLY dbo.uf_DateCalc(@SeedDate, x.DateCode) y
83+
END;
7384

7485
RETURN;
7586
END;

0 commit comments

Comments
 (0)