1

Was wondering what would be the most efficient way to get data from today, does converting the date something like

WHERE CONVERT(CHAR(8),[DateTimeColumn],112) = CONVERT(CHAR(8),GETDATE(),112)

Not slow down the process as SQL essentially has to do a RBAR compare? Any thoughts or suggestions?

Please note that the column is datetime and it contains record insert date and time

JPVoogt
  • 223
  • 2
  • 10
  • 2
    If the column is datetime, you can use WHERE CAST(DateColumn AS DATE) = CAST(GETDATE() AS DATE) for 2008+. But WHERE DateColumn >= CAST(GETDATE() AS DATE) AND DateColumn < DATEADD(day, 1, CAST(GETDATE() AS DATE)) should work for any version and can use an index. – ypercubeᵀᴹ Dec 09 '14 at 11:53
  • (well, "any version" above shoudl be any version with date type so it would only work with 2008+). For 2000 or 2005, the stripping of the time must be done differently.) – ypercubeᵀᴹ Dec 09 '14 at 12:28
  • No, use an open-ended range. WHERE col >= @today and col < dateadd(day,1,@today) - see https://sqlblog.org/2009/10/16/bad-habits-to-kick-mis-handling-date-range-queries – Aaron Bertrand Dec 09 '14 at 12:53
  • Can you please confirm whether the datetime column contains time, so that people don't provide answers that may be very wrong if their assumptions are incorrect. The more logical assumption is yes, it contains time, because why else would it be datetime and not date, and why would you bother converting the column? – Aaron Bertrand Dec 09 '14 at 13:27

0 Answers0