• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 593
  • Last Modified:

MSSQL DATEADD to get last and first day of previous month minus 1 year


this statement gives me the first day of the previous month:

SELECT DATEADD(dd,-(DAY(DATEADD(mm,-1,getdate()))-1),DATEADD(mm,-1,getdate()))

and this one the last day:

SELECT DATEADD(dd,-(DAY(getdate())),getdate())


How can I take it a step further and get the first and last day of the previous month from last year?  
0
RustyZ32
Asked:
RustyZ32
1 Solution
 
Daniel WilsonCommented:
So ... since this is October 5, 2011, you would be looking for September 1st - 30th, 2010?

SELECT DATEADD(yy, -1, DATEADD(dd,-(DAY(DATEADD(mm,-1,getdate()))-1),DATEADD(mm,-1,getdate())))

SELECT DATEADD(yy, -1,DATEADD(dd,-(DAY(getdate())),getdate()))
0
 
RustyZ32Author Commented:
that worked, thanks.
0
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.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now