Allan
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!
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!
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.
The first sql gets the first day of the current month?
the second sql gets the first day of last month.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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(v archar(6), getdate() - day(getdate()),112)+'01 00:00:00') as start_of_prior_month, dateadd(dd,-1,convert(varc har(6),get date(),112 )+'01 23:59:59') as end_of_prior_month
select convert(datetime,convert(v
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)
that's a good point mwvisa1 about the time stamp, I was gonna solve that with DATEADD(dd,1,@EndDt)
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_mont h
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:
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_mont
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
ASKER
ara ara ... sorry .. already awarded the points...
Thank you for your posts.
Thank you for your posts.
dateadd(month, -1, dateadd(day, 1-datepart(day, getdate()), convert(datetime, convert(varchar(10), getdate(), 120), 120))) first_day_of_prev_month