Technical Article

Last day of the month

,

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

Rate

1 (1)

You rated this post out of 5. Change rating

Share

Share

Rate

1 (1)

You rated this post out of 5. Change rating