SQL Server 十六进制处理
我相信 SQL Server 2000 处理十六进制数字的方式有问题。
如果我执行一个
select * from table where [timestamp] = 44731446
操作,它返回的行将时间戳显示为 0x0000000202AA8C36
同样在另一个表中,如果我
select * from table2 where [timestamp] = 44731446
返回的行将时间戳显示为 0x0000000002AA8C36 (注意缺少 2)
MS Calc 告诉我第一个时间戳 = 8634666038(十进制),第二个时间戳 = 44731446(十进制),与我对两个表的原始查询相匹配。
那么为什么 SQL Server 返回不同的数字,但查询成功呢?我相信这是我遇到的更新问题的途径,其中行不会更新。
I believe I'm having a problem with the way SQL Server 2000 handles hexadecimal numbers.
If I do a
select * from table where [timestamp] = 44731446
the row that it returns shows the timestamp as 0x0000000202AA8C36
Equally in another table if I
select * from table2 where [timestamp] = 44731446
the row that it returns shows the timestamp as 0x0000000002AA8C36 (notice the missing 2)
MS Calc tells me that the first timestamp = 8634666038 in decimal and the second timestamp = 44731446 in decimal which matches my original query on both tables.
So why is SQL Server returning a different number, yet successfully querying it? I believe this is the route of an update problem I'm having where the row won't update.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
长话短说,二进制到整数的转换会截断数据:
TIMESTAMP 列实际上是 BINARY(8),因此您的查询是将 BINARY(8) 值与 INT 值进行比较;由于 INT 具有更高的优先级,因此 MSSQL 在比较之前将 BINARY(8) 值转换为 INT。
但是,0x0000000202AA8C36(或8634666038)太大而无法表示为INT,因此MSSQL必须先截断它,并且它截断为与0x0000000002AA8C36相同的值。这可能更清楚一点:
根据在线图书(2008 年,我没有 2000 年):
Long story short, the binary to integer conversion is truncating data:
A TIMESTAMP column is really BINARY(8), so your query is comparing a BINARY(8) value to an INT value; because INT has the higher precedence, MSSQL converts the BINARY(8) value to INT before comparing them.
But, 0x0000000202AA8C36 (or 8634666038) is too big to be represented as INT, so MSSQL has to truncate it first, and it truncates to the same value as 0x0000000002AA8C36. This might be a little clearer:
According to Books Online (for 2008, I don't have 2000):