簡體   English   中英

where 子句中的 DateTime 條件無法正常工作

[英]DateTime condition in where clause doesnt work correctly

我的表中有第一列日期時間類型的行:

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

當我使用下面的選擇命令時,我得到相同的結果(這一行):

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 Server 是版本 14

DATETIME的精度為(大約)0.003 秒。 這意味着它只能表示每 3千分之一秒,其他所有內容都四舍五入為 0.000、0.003 或 0.007 秒的增量,如下表所示。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM