Solved

SQL Previous month and YTD

Posted on 2013-01-24
6
1,022 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Suggested Solutions

There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
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 …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

685 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