?
Solved

Select - past 3 months

Posted on 2011-09-12
4
Medium Priority
?
289 Views
Last Modified: 2012-06-21
I have a column called "Shippeddate" and it is datetime. I want to get the past three months only.
Do we use datepart?
0
Comment
Question by:VBdotnet2005
[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
  • 2
  • 2
4 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 252 total points
ID: 36525430
You can use DATEPART; however, I would not recommend that. I would instead, do something like this:

Shippeddate >= DATEADD(MONTH, -3, GETDATE())

Based on how I need the data, I might tweak from there, but I would start with that.
0
 
LVL 32

Assisted Solution

by:Ephraim Wangoya
Ephraim Wangoya earned 248 total points
ID: 36526565
consider getting the first day of the month then subtracting three months

where Shippeddate > DATEADD(MM, -3, DATEADD(DD, -(DAY(GETDATE())-1), GETDATE()))
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 36526572
You could remove the time part as well
declare @requireddate datetime

set @requireddate = DATEADD(DD, 0, DATEDIFF(DD, 0, GETDATE()))
set @requireddate = DATEADD(MM, -3, DATEADD(DD, -(DAY(@requireddate)-1), @requireddate))

.....
where Shippeddate >= @requireddate

Open in new window

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36526630
If you want to go back to the first day of the month three months ago, you can do that like this:

WHERE ShippedDate >= DATEADD(MM, DATEDIFF(MM, 0, GETDATE())-3, 0)

It strips the time and gets you to the first day of the month at the same time.
0

Featured Post

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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 datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

765 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