Solved

SQL Previous month and YTD

Posted on 2013-01-24
6
1,006 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
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:

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
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.
0
 
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 ?
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 1

Author Comment

by:LCNW
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.
0
 
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

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
ID: 38836535
I haven't been able to figure out the code for the second part of my logic. Thanks for the help.
0

Featured Post

[Webinar] Disaster Recovery and Cloud Management

Learn from Unigma and CloudBerry industry veterans which providers are best for certain use cases and how to lower cloud costs, how to grow your Managed Services practice in IaaS clouds, and how to utilize public cloud for Disaster Recovery

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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.
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

12 Experts available now in Live!

Get 1:1 Help Now