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?
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
arof

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Brendt Hess

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
_agx_


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
_agx_

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
arof

ASKER
thanks all..
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck