2011-05-31 55 views
0

嘿家伙, 我已经创建了一个存储过程,其中我在select查询中有datetime字段。 这里是我的查询是什么如何在SQL服务器的where子句中传递datetime字段的值

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

这是可能的,或者我应该如何形成一个查询我的目标是查询应返回的记录,10日至2011年

回答

1

使用CONVERT

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

尝试

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

希望得到这个帮助。

0

这样做的最好的方法是:

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

通过分离这样的条件,你允许SQL Server使用的publishdate列的索引(如果索引存在)。我显示的查询将使用超快速索引查找而不是扫描,从而获得更好的性能。