绝对简单的SQL Server查询不会返回任何内容

时间:2013-03-10 19:58:18

标签: sql-server date sql-server-2005-express

我在这里对我来说很奇怪......

Select BARCODE, DATA 
from HISTORIA 
WHERE BARCODE='25405187' AND DATA = '2013-03-08 00:00:00.000'

Select BARCODE, DATA 
from HISTORIA 
WHERE BARCODE='25405187'

简单吧?

现在请查看返回的数据:

enter image description here

Ekhmmm ......为什么......为什么......为什么? :)当你看到正确的日期,但比较会导致结果。

我有SQL Server 2005 Express和Management Studio Express

2 个答案:

答案 0 :(得分:3)

在投射到'2013-03-08 00:00:00.000'时,

datetimenot an unambiguous format。它可以是8月3日或3月8日。

这取决于您的DATEFORMAT设置(这取决于您登录的语言)。

SET LANGUAGE english

SELECT CAST('2013-03-08 00:00:00.000' AS DATETIME) AS english

SET LANGUAGE british

SELECT CAST('2013-03-08 00:00:00.000' AS DATETIME) AS british

返回

Changed language setting to us_english.
english
-----------------------
2013-03-08 00:00:00.000

Changed language setting to British.
british
-----------------------
2013-08-03 00:00:00.000

使用

WHERE BARCODE='25405187' AND DATA = '20130308 00:00:00.000' 

或更简单

WHERE BARCODE='25405187' AND DATA = '20130308' 

答案 1 :(得分:1)

尝试此标准(ISO)日期表示:

Select BARCODE, DATA 
from HISTORIA 
WHERE BARCODE='25405187' AND DATA = '2013-03-08T00:00:00.000'