Get All Dates of Given Month and Year in SQL Server

后端 未结 11 1516
梦如初夏
梦如初夏 2021-02-19 11:22

I want to get all dates by declaring month and year in SQL server.

Can anyone please share easy lines of SQL code to get it.

For example:



        
11条回答
  •  醉话见心
    2021-02-19 11:45

    Same approach as t-clausen, but a more compact:

    Declare @year int = 2017, @month int = 11;
    WITH numbers
    as
    (
        Select 1 as value
        UNion ALL
        Select value + 1 from numbers
        where value + 1 <= Day(EOMONTH(datefromparts(@year,@month,1)))
    )
    SELECT datefromparts(@year,@month,numbers.value) Datum FROM numbers
    

提交回复
热议问题