Solved

SQL Previous month and YTD

Posted on 2013-01-24
6
997 Views
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?

Thanks.
0
Comment
Question by:LCNW
  • 3
  • 3
6 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 500 total points
Comment Utility
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:

http://sqlandme.com/2011/05/17/how-to-get-first-and-last-day-of-year-tsql/

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)
0
 
LVL 1

Author Comment

by:LCNW
Comment Utility
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.
0
 
LVL 22

Expert Comment

by:Steve Wales
Comment Utility
If you're wanting YTD based upon the current date instead of the beginning date, just use getdate() as shown in the blog post ?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 1

Author Comment

by:LCNW
Comment Utility
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.
0
 
LVL 22

Assisted Solution

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

http://msdn.microsoft.com/en-us/library/ms174420.aspx

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.
0
 
LVL 1

Author Comment

by:LCNW
Comment Utility
I haven't been able to figure out the code for the second part of my logic. Thanks for the help.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

8 Experts available now in Live!

Get 1:1 Help Now