Link to home
Start Free TrialLog in
Avatar of arof
arof

asked on

SQL SERVER & Date function

I need a function that returns the last day on the previous month of given date . any idea?

example:

if date = 20070530, then, the last day of previous month is 20070430
if date = 20070630, then, the last day of previous month is 20070531

and so on..

returns: datetime
parameter: datetime


any idea?
ASKER CERTIFIED SOLUTION
Avatar of Brendt Hess
Brendt Hess
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

create function dbo.ReturnLastDayOfPrevMonth (@theDate varchar(10))
returns datetime
as
begin
      return dateadd(d, -1, convert(datetime, left(@theDate, 6) +'01', 112))
end
go

select dbo.ReturnLastDayOfPrevMonth('20070530')
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of arof
arof

ASKER

thanks all..