Get the last date of the month for the given date.
Algorithm is:
1. Transform given date in “first day of the month”
2. Add a month to result
3. Subtract a day from second result
2007-10-02 (first published: 2002-06-20)
15,451 reads
Get the last date of the month for the given date.
Algorithm is:
1. Transform given date in “first day of the month”
2. Add a month to result
3. Subtract a day from second result
create function LastDayofMonth(@dt datetime) returns datetime as begin returndateadd( day ,-1 ,dateadd( month ,1 ,str(year(@dt),4)+'-'+str(month(@dt),2)+'-01' ) ) end Go -- example select dbo.LastDayofMonth('2004-02-03') -- result LastDay ------------------------------------------------------ 2004-02-29 00:00:00.000