Skip to content

Instantly share code, notes, and snippets.

@IcedMango
Created December 16, 2021 05:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save IcedMango/35f40e36b674bf3c27212d004d20f871 to your computer and use it in GitHub Desktop.
Save IcedMango/35f40e36b674bf3c27212d004d20f871 to your computer and use it in GitHub Desktop.
sql function getRangeMonthList
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