?
Solved

Go back 6 months in sql server

Posted on 2011-09-20
7
Medium Priority
?
450 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 93

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 93

Accepted Solution

by:
Patrick Matthews earned 2000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 21

Expert Comment

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

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 93

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
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 shrink a transaction log file down to a reasonable size.

650 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