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:
DECLARE @month AS INT = 5
DECLARE @Year AS INT = 2016
SELECT * from Something
I have tried below things,
DECLARE @month TINYINT=5
;WITH CTE_Days AS (
SELECT DATEADD(
MONTH,
@month,
DATEADD(
MONTH,
-MONTH(GETDATE()),
DATEADD(
DAY,
-DAY(GETDATE()) + 1,
CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME)
)
)
) Dates
UNION ALL
SELECT DATEADD(DAY, 1, Dates)
FROM CTE_Days
WHERE Dates < DATEADD(
DAY,
-1,
DATEADD(
MONTH,
1,
DATEADD(
MONTH,
@month,
DATEADD(
MONTH,
-MONTH(GETDATE()),
DATEADD(
DAY,
-DAY(GETDATE()) + 1,
CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME)
)
)
)
)
)
)
SELECT Dates
FROM CTE_Days
But I am looking for easy solution with less lines and short answer