Created
December 16, 2021 05:42
-
-
Save IcedMango/35f40e36b674bf3c27212d004d20f871 to your computer and use it in GitHub Desktop.
sql function getRangeMonthList
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE FUNCTION getRangeMonthList(@beginDate VARCHAR(50), @endDate VARCHAR(50)) | |
RETURNS @table TABLE | |
( | |
MonthList VARCHAR(20) | |
) | |
AS | |
BEGIN | |
WITH x AS | |
( | |
SELECT CAST(@beginDate AS DATE) [Month] | |
UNION ALL | |
SELECT DATEADD(M, 1, [Month]) | |
FROM x | |
WHERE [Month] < CAST(@endDate AS DATE) | |
) | |
INSERT | |
INTO @table | |
SELECT CONVERT(VARCHAR(7), [Month], 126) AS MonthList | |
FROM x | |
WHERE CONVERT(VARCHAR(7), [Month], 126) < @endDate | |
RETURN | |
END | |
GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment