开发者

how to pass the value for datetime field in where clause in sql server

开发者 https://www.devze.com 2023-03-10 00:13 出处:网络
hey guys, i have created a stored procedure, wherein i have datetime field in the select query. here is what my query is

hey guys, i have created a stored procedure, wherein i have datetime field in the select query. here is what my query is

select * from tablename where publishdate like '%03/10/2011%'

is this 开发者_如何学运维possible, or how should i form a query my objective is query should return the records for 10-March-2011


Using CONVERT

select * from tablename where convert(varchar(10), publishdate, 103) = '03/10/2011'


Try

select * from tablename where day(publishdate) = 10 And Month(publishdate) = 3
And Year(publishdate) = 2011 --Let the required date be 10th March 2011

Hope this help.


The best method for doing this is:

Select * 
from   tablename
Where  publishdate >= '20110310'
       and publishdate < '20110311'

By separating the conditions like this, you are allowing SQL Server to use an index on the publishdate column (if an index exists). The query I show would use a super-fast index seek instead of a scan, resulting in far better performance.

0

精彩评论

暂无评论...
验证码 换一张
取 消