Say I have a table which stores customers order IDs. Such as
| Customer ID | Order ID | Order Date
How can I get all customers who have ordered today?
Also OrderDate would be a DateTime.
Something like
SELECT DISTINCT CustomerID
FROM TableName
Where OrderDate > Today
But the last part is what I can’t figure out.
It’s fairly common to only want a date out of a datetime – you should be able to Google for the specifics of your RDBMS (since you don’t mention it). The important bit is to make your query SARGable by transforming today’s date1 – not the order date.
For MSSQL, something like
would work by taking number of days today is from Date 0 (
DATEDIFF(dd, 0, GETDATE())) and adding them back to Date 0 (DATEADD(dd, 0, x)). That’s T-SQL specific, though.1 If you were searching for an arbitrary date, you’d still transform both arguments: