意外的查询结果 [英] Unexpected query results
问题描述
为什么我从 sql-server 得到以下结果?
Why I get following results from sql-server?
SELECT '' + 12 C1, CONVERT(int, '') C2,
CASE WHEN '' = ' ' THEN 'equal' ELSE 'not equal' END C3
--Results
| C1 | C2 | C3 |
-------------------
| 12 | 0 | equal |
C3 已得到答复.每个人都回答认为 '' + 12 = 12
是字符串连接,但它是一种数学运算.目前尚不清楚为什么(NOT 如何)''
在 sql-server 中转换为 0.
C3 has been answered. Everyone answered thinking '' + 12 = 12
is string concatenation but it is a mathematical operation. It is not clear why (NOT how) ''
is converting to a 0 in sql-server.
推荐答案
SQL Server 有一个奇怪的特性,如果比较两个字符串,并且它们的长度不相等,那么它会将较短的一个填充到较长的一个,然后比较它们.填充字符是一个空格.http://support.microsoft.com/kb/316626
SQL Server has a strange feature whereby, if two strings are compared, and they are not of equal length, then it will pad the shorter one to the length of the longer one and then compare them. The padding character is a space. http://support.microsoft.com/kb/316626
C1 &C2 只是隐含的 &将空字符串显式转换为 0.
C1 & C2 are just implicit & explicit conversion of an empty string to 0.
这篇关于意外的查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!