当前位置: 代码迷 >> Sql Server >> 日期类型的精确匹配解决办法
  详细解决方案

日期类型的精确匹配解决办法

热度:27   发布时间:2016-04-27 17:28:26.0
日期类型的精确匹配
我对一个日期字段查询,希望对它精确匹配
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 ' )
这样试试
  相关解决方案