Solved

Go back 6 months in sql server

Posted on 2011-09-20
7
387 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

803 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