clear markdown compare help best fiddles feedback
clear markdown feedback
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0. 3601446 fiddles created (47983 in the last week).

DECLARE @Months TABLE( Date DATETIME, Year INT, Month INT, LastDay INT ) DECLARE @cnt INT = 1, @Date DATETIME; WHILE @cnt <= 12 BEGIN SELECT @Date = DATEADD(DAY, -1, CONVERT(DATETIME, CONVERT(NVARCHAR(500), YEAR(GETDATE()) + IIF(@cnt = 12, 1, 0)) + '.' + CONVERT(NVARCHAR(2), IIF( @cnt = 12, 1, @cnt + 1)) +'.1')) INSERT INTO @Months(Date, Year, Month, LastDay) VALUES(@Date, YEAR(@Date), MONTH(@Date), DAY(@Date)) SET @cnt = @cnt + 1; END SELECT * FROM @Months
Date Year Month LastDay
2021-01-31 00:00:00.000 2021 1 31
2021-02-28 00:00:00.000 2021 2 28
2021-03-31 00:00:00.000 2021 3 31
2021-04-30 00:00:00.000 2021 4 30
2021-05-31 00:00:00.000 2021 5 31
2021-06-30 00:00:00.000 2021 6 30
2021-07-31 00:00:00.000 2021 7 31
2021-08-31 00:00:00.000 2021 8 31
2021-09-30 00:00:00.000 2021 9 30
2021-10-31 00:00:00.000 2021 10 31
2021-11-30 00:00:00.000 2021 11 30
2021-12-31 00:00:00.000 2021 12 31
 hidden batch(es)