SQL: How to display records for only 30 days

Stanley picture Stanley · Mar 2, 2009 · Viewed 12.4k times · Source

I tried to use

SELECT * from Results
WHERE DATEDIFF(d,Date,getdate())<30

But it seem having error with this.

For each record submitted will only display 30 days. May I know if my syntax is correct?

Greatly Appreciated, Stan

Answer

Mitch Wheat picture Mitch Wheat · Mar 2, 2009

Syntax looks OK, but you might need to 'quote' Date:

SELECT * from Results WHERE DATEDIFF(d, [Date], getdate()) < 30

Do you have a column called Date in Results?

BTW, that won't be able to use an index, whereas this will:

SELECT * from Results WHERE [Date] >= DATEADD(d, -30, getdate())