问题
How can I exclude in my query that rows that have status is Pending and the Creation Date is less than the current date ?
[Table]
Status CreationDate
Pending 9/30/2014 9:00 PM
Completed 9/14/2014 10:52 AM
Pending 9/30/2014 10:00 PM
回答1:
Select * FROM tableA
WHERE status not like 'Pending'
And CreationDate < GETDATE()
回答2:
SELECT * FROM sometable
WHERE Status != 'Pending'
AND DATE(CreationDate) = GETDATE();
As you creation date is a timestamp or datetime field, you want to cast this to Date, so you can compare with the value returned by GetDate which returns a date, not datetime. So, this query only includes those records where status is not pending and the creation date is today, which is the opposite of your exclusion statement.
I could not make you a SQL Fiddle of this, as SQL Fiddle does not support Sybase.
Note, casting to date is considered Sargable in Sybase, which means it will make efficient use of an index on your creationdate field -- which you should have I you have non-trivial table sizes and plan to run this kind of query a lot.
来源:https://stackoverflow.com/questions/26112524/how-can-i-exclude-in-my-query-that-rows-that-have-status-is-pending-and-the-crea