确认SQL Server错误?

时间:2014-12-04 01:42:17

标签: sql-server

如果在SQL Server 2014中运行以下查询,您会得到DT列的结果?它应该是“1”,但在SQL Server 2008中,我得到“0”。

SELECT
    CASE WHEN CAST('2014-01-02 00:00:00.001' AS DATETIME) > CAST('2014-01-02 00:00:00.000' AS DATETIME) THEN 1 ELSE 0 END AS 'DT'
    , CASE WHEN CAST('2014-01-02 00:00:00.002' AS DATETIME) > CAST('2014-01-02 00:00:00.000' AS DATETIME) THEN 1 ELSE 0 END AS 'DT2'
    , CASE WHEN '2014-01-02 00:00:00.001' > '2014-01-02 00:00:00.000' THEN 1 ELSE 0 END AS 'VARCHAR'

1 个答案:

答案 0 :(得分:4)

日期时间字段的分辨率为 3个小数位。只能在3.33毫秒内准确

SQL Server将'2014-01-02 00:00:00.001''2014-01-02 00:00:00.000'都视为2014-01-02 00:00:00.000

SELECT Cast('2014-01-02 00:00:00.001' AS DATETIME),
       Cast('2014-01-02 00:00:00.000' AS DATETIME) 

两个日期的结果相同。

在你的案例陈述中CAST('2014-01-02 00:00:00.001' AS DATETIME) > CAST('2014-01-02 00:00:00.000' AS DATETIME)失败了,因为两者都是相同的,所以其他部分0都有效。