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

Posted on 2011-10-05
Last Modified: 2012-05-12

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?  
Question by:RustyZ32
    LVL 32

    Accepted Solution

    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()))
    LVL 6

    Author Closing Comment

    that worked, thanks.

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
    JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
    Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
    Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now