moe
moe

Reputation: 5249

How to get all records for the last 2 days in SQL

I am trying to get all the records from the last 2 days but excluding today's date. I want to get name and location where date submitted is included for the last 2 days only. The data type for date_submitted is datetime.

select name, location 
from myTable 
where date_submitted in (select CAST(FLOOR(CAST(GETDATE() AS FLOAT)) AS DATETIME))

Upvotes: 0

Views: 22731

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 156978

Don't use in, use >=. You can also use dateadd:

where date_submitted >= cast(dateadd(day, -2, getdate()) as date)
and   date_submitted < cast(getdate() as date)

Upvotes: 11

Related Questions