SQL Previous month and YTD

Posted on 2013-01-24
Last Modified: 2013-01-30
I need to expand on a solution that I received here. I wanted to get the past 3 months of data from the current date with each month separate. For example:

(@MyDate>= DATEADD(month, DATEDIFF(month, 0, GETDATE()) - 2, 0)) AND (@MyDate< DATEADD(month, DATEDIFF(month, 0, GETDATE()) - 1, 0))

This would get data from 2 months ago. I'm now being tasked to get data from 2 months ago plus ytd at that point in time. So, November 2012 and January 2012 - November 2012. I need this to be dynamic as the months shift. Any suggestions?

Question by:LCNW
  • 3
  • 3
LVL 22

Accepted Solution

Steve Wales earned 500 total points
ID: 38816122
Here's a nice little blog post that expands upon the above and shows you how to get first and last days of this, last and next year:

If you need to get the first of the year starting 2 months ago, instead of using "getdate()" in the calculation for "first of the year" you'd use your starting calc of "DATEADD(month, DATEDIFF(month, 0, GETDATE()) - 2, 0))"

So first of the year, starting at your starting date would be

DATEADD(YEAR, DATEDIFF(YEAR, 0, DATEADD(month, DATEDIFF(month, 0, GETDATE()) - 2, 0))), 0)

Author Comment

ID: 38816971
This works great until I start a new year. I need it to reset at that point. For instance, in January it does January 2013 back to January 2012. I would only want January 2013 back to January 2013 as it's a new year.
LVL 22

Expert Comment

by:Steve Wales
ID: 38817005
If you're wanting YTD based upon the current date instead of the beginning date, just use getdate() as shown in the blog post ?
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud


Author Comment

ID: 38819673
I think I need to do something where I say AND year=year. This way it keeps the YTD on the correct year. I'm just not sure how to code it.
LVL 22

Assisted Solution

by:Steve Wales
Steve Wales earned 500 total points
ID: 38819744
You can extract year from a date using the DATEPART function

DATEPART(yy, getdate())

Set your variable or where clause based upon that and you'd be set.

where DATEPART(yy, YourColumn) = DATEPART(yy, @YourVariable).

Just remember that using a function on the left column of a where clause like that upsets the optimizer and it can't use an index to fulfill the requirement (unless you have othere conditions in there that force the index search first.

Author Comment

ID: 38836535
I haven't been able to figure out the code for the second part of my logic. Thanks for the help.

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Server Question 5 36
tempdb log contention 16 38
T-SQL - How to use like '%%' operator to search an integer 2 25
Query to return total 6 18
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

20 Experts available now in Live!

Get 1:1 Help Now