Solved

Select - past 3 months

Posted on 2011-09-12
4
254 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
  • 2
  • 2
4 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 63 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:ewangoya
ewangoya earned 62 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:ewangoya
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 59

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
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 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…
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…

914 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