Possible recursive CTE query using date ranges

前端 未结 3 1185
一向
一向 2020-12-21 02:39

Not sure how to even phrase the title on this one!

I have the following data:

IF OBJECT_ID (\'tempdb..#data\') IS NOT NULL DROP TABLE #data
CREATE TA         


        
3条回答
  •  隐瞒了意图╮
    2020-12-21 03:06

    DECLARE @date DATE = GETDATE()
    
    ;WITH MonthsCTE AS (
        SELECT 1 [Month], DATEADD(DAY, -DATEPART(DAY, @date)+1, @date) as 'MonthStart'
        UNION ALL
        SELECT [Month] + 1, DATEADD(MONTH, 1, MonthStart)
        FROM MonthsCTE 
        WHERE [Month] < 12 )
    
    SELECT * FROM MonthsCTE
    

提交回复
热议问题