Skip to content

Instantly share code, notes, and snippets.

@linyongfu2013
Forked from IcedMango/getRangeMonthList.sql
Created December 16, 2021 08:09
Show Gist options
  • Save linyongfu2013/94d26b15e9badb8c4d25c4d1300e2ecf to your computer and use it in GitHub Desktop.
Save linyongfu2013/94d26b15e9badb8c4d25c4d1300e2ecf to your computer and use it in GitHub Desktop.

Revisions

  1. @IcedMango IcedMango created this gist Dec 16, 2021.
    24 changes: 24 additions & 0 deletions getRangeMonthList.sql
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    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