Go back 6 months in sql server

Posted on 2011-09-20
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?

Question by:printmedia
  • 4
  • 2
LVL 92

Expert Comment

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

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


Author Comment

ID: 36570303
Thanks for the quick reply!

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

Accepted Solution

Patrick Matthews earned 500 total points
ID: 36570581

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":'t-be-scared-and-do-it-right-the-first-time.html
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

LVL 21

Expert Comment

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

Expert Comment

by:Patrick Matthews
ID: 36573476

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.


Author Comment

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

LVL 92

Expert Comment

by:Patrick Matthews
ID: 36573571

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


Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

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…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

932 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

17 Experts available now in Live!

Get 1:1 Help Now