Solved

Select - past 3 months

Posted on 2011-09-12
4
264 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
I have a large data set and a SSIS package. How can I load this file in multi threading?
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

776 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