0

Im not a sql person and have been thrust into being one :\

Im trying to get the previous 3 months of data without the current month.

WHERE DATEPART(m, start_date) = DATEPART(m, DATEADD(m, -3, getdate())) 
 and DATEPART(yyyy, start_date) = DATEPART(yyyy, DATEADD(m, -3, getdate()))

this gets me the data from January only, i need Jan-Mar.

WHERE start_date >= dateadd(mm,datediff(mm,0,getdate())-3,0)

and this gets me everything from jan 1st - now.

So now im stuck and not sure what to do. mssql ver: 2012

Thank you in advance for any help.

2
  • What do you mean by previous 3 months. The previous 3 whole calendar months? The previous quarter? In May, will you expect Feb-April data?
    – Necreaux
    Apr 15, 2015 at 20:30
  • Sorry, yes, the previous whole calendar months.
    – yazzu
    Apr 15, 2015 at 20:51

6 Answers 6

3
WHERE DateColumn >= DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE()) -3,0)
  AND DateColumn <  DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE())   ,0)

This will return the data from Between 2015-01-01 and 2015-03-31

2
  • Why not DateColumn < DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE()),0)? Aren't you just as vulnerable to the BETWEEN trap with <= EOMONTH(...)
    – Anon
    Apr 15, 2015 at 20:52
  • 2015-03-31 12:00:00
    – Anon
    Apr 15, 2015 at 21:02
1

You can try this:

Where datediff(mm, start_date, getdate()) in (1, 2, 3)

3
  • This will hurt the performance as it will not make use of any indexes on the datetime column.
    – M.Ali
    Apr 15, 2015 at 20:41
  • In fairness, the OP never mentioned anything about performance Apr 15, 2015 at 21:08
  • 1
    true, but isnt that always a consideration :)
    – yazzu
    Apr 15, 2015 at 21:12
0

Does this helps?:

WHERE start_date BETWEEN DATEADD(m, -3, getdate()) AND DATEADD(m, -1, getdate()) 
3
  • Op didn't mention about date precision. So if start_date its like '20150401 00:00' should work.
    – ericpap
    Apr 15, 2015 at 20:48
  • If you read the article you would find out between operator does not work with dates it will miss out few rows. Read the article and you would know what I mean.
    – M.Ali
    Apr 15, 2015 at 20:51
0

You can use something like this:

WHERE MONTH(start_date)>= MONTH(getdate())- 3 AND MONTH(start_date)< MONTH(getdate())
1
  • This will hurt the performance as it will not make use of any indexes on the datetime column.
    – M.Ali
    Apr 15, 2015 at 20:41
0

You can build a string and convert it to a date... such as:

WHERE start_date >= dateadd(month,-3, convert(datetime,convert(varchar(10),Month(GETDATE())) + '/01/' + convert(varchar(10),Year(GETDATE()))))
    and start_date < convert(datetime,convert(varchar(10),Month(GETDATE())) + '/01/' + convert(varchar(10),Year(GETDATE())))
0
SELECT DATEADD(MONTH,DATEDIFF(MONTH,0,DATEADD(MONTH,-3,GETDATE())),0)  -- FIRST DAY OF MONTH, THREE MONTHS AGO
      ,DATEADD(MILLISECOND,-3,DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) -- LAST DAY OF LAST MONTH

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.