Start and End Date For Previous Month

allanau20
allanau20 used Ask the Experts™
on
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!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
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

Author

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.
Topic Advisor, Page Editor
Distinguished Expert 2018
Commented:
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

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Kevin CrossChief Technology Officer
Most Valuable Expert 2011

Commented:
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
Mark WillsTopic Advisor, Page Editor
Distinguished Expert 2018

Commented:
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

Author

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)

Author

Commented:
thank you all for your posts!
Guy Hengel [angelIII / a3]Billing Engineer
Most Valuable Expert 2014
Top Expert 2009

Commented:
>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

Author

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

Thank you for your posts.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial