I am writing a User defined function in TSQL trying to get the last day of the month regardless of the date input. I am struggling with this as I am a newbie. Any help will be very much appreciated.
Here is my function:
Alter Function dbo.FN_Get_Last_Day_in_Month2
(@FN_InputDt Datetime)
Returns smalldatetime
as
Begin
Declare @Result smalldatetime
Set @Result =
case when @FN_InputDt <> 01-01-1900 then
DATEADD(m, DATEDIFF(M, 0,@FN_InputDt)+1, -1)
Else 0 End
Return @Result
End
Here is the test
select dbo.fn_get_last_day_in_month (07-05-2010)
here is the returned result:
2010-07-31 00:00:00
(which of course is really wrong)