T-SQL Query Returning Items It Shouldn't Be

前端 未结 5 893
遥遥无期
遥遥无期 2021-01-21 17:15

Here\'s the scenario. The column in question is called \'datein\' and it\'s type is \'datetime\'. I have three rows with the value of \'2009-10-01 00:00:00.000\' for \'datein\

5条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-21 17:37

    The DATETIME accuracy is 0.00333 seconds. So you need to go to '2009-09-30 23:59:59.998' so it doesn't round up to Oct 1st.

    For example:

    select '2009-09-30 23:59:59.994', 
      cast('2009-09-30 23:59:59.994' as datetime)
    union all select '2009-09-30 23:59:59.995', 
      cast('2009-09-30 23:59:59.995' as datetime)
    union all select '2009-09-30 23:59:59.996', 
      cast('2009-09-30 23:59:59.996' as datetime)
    union all select '2009-09-30 23:59:59.997', 
      cast('2009-09-30 23:59:59.997' as datetime)
    union all select '2009-09-30 23:59:59.998', 
      cast('2009-09-30 23:59:59.998' as datetime)
    union all select '2009-09-30 23:59:59.999', 
      cast('2009-09-30 23:59:59.999' as datetime)
    

    returns:

    2009-09-30 23:59:59.994 2009-09-30 23:59:59.993
    2009-09-30 23:59:59.995 2009-09-30 23:59:59.997
    2009-09-30 23:59:59.996 2009-09-30 23:59:59.997
    2009-09-30 23:59:59.997 2009-09-30 23:59:59.997
    2009-09-30 23:59:59.998 2009-09-30 23:59:59.997
    2009-09-30 23:59:59.999 2009-10-01 00:00:00.000
    

提交回复
热议问题