[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Showing purchases made within the previous 8 months using SQL

Posted on 2011-10-17
6
Medium Priority
?
234 Views
Last Modified: 2012-06-21
I am trying to find purchases made within the previous 8 months. This does not include the current month but starting 1 month before the current month up to 8 months.

I have figured out how to find the purchases made in the last 8 months (query below), but how do I exclude that current month.

SELECT prod_id,
         customer_name,
         purchase_date
FROM puchase_orders
WHERE purchase_date > (DATEADD(m, -6, GETDATE()))

 
0
Comment
Question by:Jintonix415
  • 4
6 Comments
 
LVL 15

Accepted Solution

by:
Anuj earned 2000 total points
ID: 36983876
For last 6 months excluding previous month

SELECT prod_id,
         customer_name,
         purchase_date
FROM puchase_orders
WHERE purchase_date BETWEEN  DATEADD(mm,-7,CONVERT(VARCHAR,GETDATE(),112)) AND DATEADD(mm,-1,CONVERT(VARCHAR,GETDATE(),112))
0
 
LVL 15

Expert Comment

by:Anuj
ID: 36983882


SELECT prod_id,
         customer_name,
         purchase_date
FROM puchase_orders
WHERE purchase_date BETWEEN  DATEADD(mm,-7,GETDATE()) AND DATEADD(mm,-1,GETDATE())
0
 
LVL 15

Expert Comment

by:Anuj
ID: 36983884
Use between, for retrieving 8 months data from previous month use  purchase_date BETWEEN  DATEADD(mm,-9,GETDATE()) AND DATEADD(mm,-1,GETDATE())
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:Jintonix415
ID: 36983886
anujnb. thank you I think that worked. But quick question, what is the 112 value and why did you need to add Convert?
0
 
LVL 15

Expert Comment

by:Anuj
ID: 36983905
Just to remove the time part.
 
0
 
LVL 9

Expert Comment

by:sachinpatil10d
ID: 36983909
Try this
SELECT prod_id,
         customer_name,
         purchase_date
FROM puchase_orders
WHERE purchase_date between convert(datetime, convert(nvarchar,(MONTH(getdate())-8)) + '-01-' + CONVERT(nvarchar,YEAR(getdate())))
and convert(datetime, convert(nvarchar,(MONTH(getdate())-1)) + '-01-' + CONVERT(nvarchar,YEAR(getdate())))

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

825 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