Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


SQL Previous month and YTD

Posted on 2013-01-24
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 23

Accepted Solution

Steve Wales earned 2000 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 23

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 ?
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks


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 23

Assisted Solution

by:Steve Wales
Steve Wales earned 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

618 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