日期:2014-05-18  浏览次数:20453 次

日期类型的精确匹配
我对一个日期字段查询,希望对它精确匹配
Select   *   from   tablename   where   Birthday= '2005-1-1   19:12:12 '   ,但是表里面明明有这一行,但是却检索不出来
用Select   *   from   tablename   where   (DATEDIFF(ss,   Birthday,   CAST( '2005-1-1   19:12:12 '     AS   datetime))   =   0),还是检索不出来,
但是如果用用Select   *   from   tablename   where   (DATEDIFF(ss,   Birthday,   CAST( '2005-1-1   19:12:11 '     AS   datetime))   =   0),也就是少一秒钟的时间,就可以检索出来了,这是为什么呢?

------解决方案--------------------
Birthday是不是datetime类型
------解决方案--------------------
字段Birthday 是datatime类型吧,是 不是因为Birthday字段带毫秒啊, '2005-1-1 19:12:12 ' 不带毫秒
------解决方案--------------------

Select * from tablename
where Convert(nvarchar(19),Convert(datetime,Birthday),20)=Convert(datetime, '2005-1-1 19:12:12 ' )
这样试试