Start and End Date For Previous Month

Hi Experts,

How do you get the first and last day of the previous month based on the getdate()?

today is 11/18/2008, so

the Start Date (first day of the previous month) = 10/01/2008
the End Date (last day of the previous month) = 10/31/2008

This is for MS-SQL 2005.

TIA!
LVL 5
allanau20Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
select dateadd(day, 1-datepart(day, getdate()), convert(datetime, convert(varchar(10), getdate(), 120), 120)) first_day_of_this_month
dateadd(month, -1, dateadd(day, 1-datepart(day, getdate()), convert(datetime, convert(varchar(10), getdate(), 120), 120))) first_day_of_prev_month
0
allanau20Author Commented:
thanks angelIII for your help.

The first sql gets the first day of the current month?

the second sql gets the first day of last month.
0
Mark WillsTopic AdvisorCommented:
or , using getdate() as the date source, can do :


select convert(datetime,convert(varchar(6),getdate() - day(getdate()),112)+'01') as start_of_prior_month ,dateadd(dd,-1,convert(varchar(6),getdate(),112)+'01') as end_of_prior_month

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Kevin CrossChief Technology OfficerCommented:
SELECT        
DATEADD(mm, - 1, DATEDIFF(dd, 0, GETDATE() - DAY(GETDATE()) + 1)) AS FirstDayOfLastMonth
, DATEADD(mm, 0, DATEDIFF(dd, 0, GETDATE() - DAY(GETDATE()))) AS LastDayOfLastMonth

This gets you both days at midnight.  if you are using this to include values from last month, I would use like this:

dateField >= DATEADD(mm, - 1, DATEDIFF(dd, 0, GETDATE() - DAY(GETDATE()) + 1))
AND dateField < DATEADD(mm, 0, DATEDIFF(dd, 0, GETDATE() - DAY(GETDATE()) + 1))

Which goes from midnight of 10/1 inclusive to all dates less than 11/1.

If you just need the dates without the time, then you can use the convert Angel showed with this:

DATEADD(mm, - 1, GETDATE() - DAY(GETDATE()) + 1)  -- first day
DATEADD(mm, 0, GETDATE() - DAY(GETDATE()))  -- last day
0
Mark WillsTopic AdvisorCommented:
and if time is important (usually is in datetime),

select convert(datetime,convert(varchar(6),getdate() - day(getdate()),112)+'01 00:00:00') as start_of_prior_month, dateadd(dd,-1,convert(varchar(6),getdate(),112)+'01 23:59:59') as end_of_prior_month

0
allanau20Author Commented:
So many Experts so I'll try to be fair.

that's a good point mwvisa1 about  the time stamp, I was gonna solve that with DATEADD(dd,1,@EndDt)
0
allanau20Author Commented:
thank you all for your posts!
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>The first sql gets the first day of the current month?
so that's fine, right?

>the second sql gets the first day of last month.
usually, you want to do a date query for

where your_date_col >= first_day_of_previous_month
   and your_date_col < first_day_of_current_month

hence, my suggestion.

if you REALLY want to get the last day of the month date value:

select dateadd(day, 1-datepart(day, getdate()), convert(datetime, convert(varchar(10), getdate(), 120), 120)) first_day_of_this_month
, dateadd(month, -1, dateadd(day, 1-datepart(day, getdate()), convert(datetime, convert(varchar(10), getdate(), 120), 120))) first_day_of_prev_month
, dateadd(day, -datepart(day, getdate()), convert(datetime, convert(varchar(10), getdate(), 120), 120)) last_day_of_previous_month

Open in new window

0
allanau20Author Commented:
ara ara ... sorry .. already awarded the points...

Thank you for your posts.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.