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:
Here is a query:
DECLARE @ReportDate VARCHAR(20)='2019-02-10'
DECLARE @LastDay INT =DAY(EOMONTH(@ReportDate))
DECLARE @DayStart INT=01
CREATE TABLE #TEMPMonth ([MDay] VARCHAR(20))
WHILE @LastDay>=@DayStart
BEGIN
INSERT INTO #TEMPMonth
SELECT CAST(CAST(YEAR(@ReportDate)AS VARCHAR)+'-'+CAST(MONTH(@ReportDate)AS VARCHAR)+'-'+CAST(@DayStart AS VARCHAR) AS DATE)
SET @DayStart+=1
END
SELECT * FROM #TEMPMonth
DROP TABLE #TEMPMonth