where 子句中的 DateTime 条件无法正常工作

DateTime condition in where clause doesnt work correctly

我的 table 中有第一列日期时间类型的行:

2021-11-01 08:51:56.123 102 296 

当我使用下面的 select 命令时,我得到相同的结果(这一行):

select * from  cmd where timestamp = convert(datetime, '2021-11-01 08:51:56.122')
select * from  cmd where timestamp = convert(datetime, '2021-11-01 08:51:56.123')
select * from  cmd where timestamp = convert(datetime, '2021-11-01 08:51:56.124')

我要求只有第二个命令选择这一行。

怎么做?

SQL 服务器版本为 14

DATETIME 的精度为(大约)0.003 秒。这意味着它只能表示每 3 千分之一秒,其他所有内容都 舍入为 .000、.003 或 .007 秒的增量,如下面的 table.[=22 所示=]

User-specified value    System stored value
01/01/98 23:59:59.999   1998-01-02 00:00:00.000
01/01/98 23:59:59.995

01/01/98 23:59:59.996

01/01/98 23:59:59.997

01/01/98 23:59:59.998   1998-01-01 23:59:59.997
01/01/98 23:59:59.992

01/01/98 23:59:59.993

01/01/98 23:59:59.994   1998-01-01 23:59:59.993
01/01/98 23:59:59.990

01/01/98 23:59:59.991   1998-01-01 23:59:59.990

您可以使用 DATETIME2(3) 而不是 DATETIME 以获得更高的精度。

详情请阅读https://docs.microsoft.com/en-us/sql/t-sql/data-types/datetime-transact-sql?view=sql-server-ver15