Solved

Go back 6 months in sql server

Posted on 2011-09-20
7
377 Views
Last Modified: 2012-06-27
Hi all.

I want to be able to go back exactly 6 months from my current month in my orderdate field of myTableA. I've tried using
DATEADD(month, -6, GetDate())

But it'll pull records starting from let's say 03/20/2011 till today. But I want it to include ALL days in March so orderdates starting 03/01/2011 until today. I want to get records from the entire month, basically ignore the day and just look at the month (Sept, Aug, July, June, May, April and March). And if the current month is February 2012 then (Feb 2012, Jan 2012, Dec 2011, Nov, etc)

Any idea how I can do this?

            
0
Comment
Question by:printmedia
  • 4
  • 2
7 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36570231
Try something like this:

SELECT *
FROM SomeTable
WHERE DateTimeColumn >= DATEADD(month, DATEDIFF(month, '2000-01-01', GETDATE()) - 6, '2000-01-01') AND
    DateTimeColumn < DATEADD(month, DATEDIFF(month, '2000-01-01', GETDATE()) - 5, '2000-01-01')

Open in new window

0
 

Author Comment

by:printmedia
ID: 36570303
Thanks for the quick reply!

Would you mind explaining the code, I want to make sure I understand it.
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 36570581
Certainly.

DATEDIFF(month, '2000-01-01', GETDATE()) tells us how many month boundaries there are between 1 Jan 2000 (nothing magical about that date; any Jan 1 will do) and the current date.  By putting that back into DATEADD using the same base date of 1 Jan 2000 we can get the beginning of the current month:

DATEADD(month, DATEDIFF(month, '2000-01-01', GETDATE()), '2000-01-01')

Of course, since you want the first of the month from 6 months ago, we have to modify that a bit:

DATEADD(month, DATEDIFF(month, '2000-01-01', GETDATE()) - 6, '2000-01-01')

Then that last expression gives us the first day of the month from 5 months ago.

Be sure to have a look at this article by EE superstar and Microsoft MVP angelIII, especially Section 6, "Date Ranges":

http://www.experts-exchange.com/Database/Miscellaneous/A_1499-DATE-and-TIME-don't-be-scared-and-do-it-right-the-first-time.html
0
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 36573179
Select * From Table wher Date Between DateAdd(month-6,Getdate()) AND Getdate()
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36573476
PatelAlpesh,

Did you even read the question?  The Asker indicated that s/he had already tried and rejected that approach, because s/he needed an entire month.

Patrick
0
 

Author Comment

by:printmedia
ID: 36573516
Thanks matthews!

I changed your code a bit because I wanted to include the current month as well.
WHERE DateTimeColumn >= DATEADD(month, DATEDIFF(month, '2000-01-01', GETDATE()) - 6, '2000-01-01') AND DateTimeColumn <= GetDate()

Open in new window

0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 36573571
printmedia,

OK, I see now--I thought you wanted just that single month :)

If there is any possibility that your table contains items for future datetimes still scheduled to occur this month, and you want to include those items:

WHERE DateTimeColumn >= DATEADD(month, DATEDIFF(month, '2000-01-01', GETDATE()) - 6, '2000-01-01') AND 
    DateTimeColumn < DATEADD(month, DATEDIFF(month, '2000-01-01', GETDATE()) + 1, '2000-01-01')

Open in new window


Patrick
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

705 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

22 Experts available now in Live!

Get 1:1 Help Now