Link to home
Start Free TrialLog in
Avatar of Allan
AllanFlag for United States of America

asked on

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!
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

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
Avatar of Allan

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Mark Wills
Mark Wills
Flag of Australia 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
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
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

Avatar of Allan

ASKER

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)
Avatar of Allan

ASKER

thank you all for your posts!
>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

Avatar of Allan

ASKER

ara ara ... sorry .. already awarded the points...

Thank you for your posts.