How can I exclude in my query that rows that have status is Pending and the Creation Date is less than the current date?

ぃ、小莉子 提交于 2019-12-25 01:44:20

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!