Solved

Select - past 3 months

Posted on 2011-09-12
4
282 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 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:Ephraim Wangoya
Ephraim Wangoya 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: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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Select Sum query with group by 8 42
Any benefit to adding a Clustered index here? 4 33
T-SQL Query 9 30
Sql server query 8 15
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

738 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