[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 582
  • 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?  
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()))
RustyZ32Author Commented:
that worked, thanks.

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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