3

I want to display records of last 4 months from current date.

I don't want to consider time

How can I get just date part from the below query?

where OrderDate >= DATEADD(month, -4, GETDATE()) 
Kara
  • 6,115
  • 16
  • 50
  • 57
Challa Jyothi
  • 241
  • 1
  • 3
  • 12

3 Answers3

5

If you're using SQL Server 2008, try converting GETDATE() to a DATE directly.

WHERE OrderDate >= DATEADD(month, -4, CONVERT(date, GETDATE())) 

http://sqlfiddle.com/#!3/df444/2

Darren
  • 68,902
  • 24
  • 138
  • 144
  • I can agree with your first, but why would you perform a conversion to varchar specifying a format that can be interpreted ambiguously and then let the system perform an implicit conversion back to `datetime`, where such ambiguity may produce incorrect results or errors? – Damien_The_Unbeliever Feb 25 '14 at 10:20
  • @Damien_The_Unbeliever - fair assumption. I have removed the latter. – Darren Feb 25 '14 at 10:21
  • Still getting date time as output...2013-10-25 15:52:31.000 – Challa Jyothi Feb 25 '14 at 10:39
0

Why not use the simple DATEDIFF function

where DATEDIFF(MM, OrderDate, GETDATE()) < 4
Hitesh
  • 3,449
  • 8
  • 39
  • 57
0

If you can't use the DATE type, there's the old way: convert the DATETIME value to CHAR, trim the hour components and then convert it back to DATETIME, so the hour components will be zeroed:

SELECT CONVERT(DATETIME, CONVERT(CHAR(8), GETDATE(), 112), 112)
-- -----------------------
-- 2014-02-25 00:00:00.000

The important thing is to use the function over the scalar parameter (and not on the column) to allow the usage of existing indexes.

Gerardo Lima
  • 6,467
  • 3
  • 31
  • 47