SQL end of month calculation

From Logic Wiki
Jump to: navigation, search


DECLARE @test DATETIME

SET @test = GETDATE() -- or any other date

SELECT DATEADD(month, ((YEAR(@test) - 1900) * 12) + MONTH(@test), -1)

https://stackoverflow.com/questions/1051488/get-the-last-day-of-the-month-in-sql

My Solution

DECLARE @aa DATETIME;

SET @aa = GETDATE();

SELECT DATEADD(DAY, -1, DATEFROMPARTS(YEAR(DATEADD(month, 1, @aa)), MONTH(DATEADD(month, 1, @aa)) , 1) )