SQL Server 2012空白字符串比较为0 [英] SQL Server 2012 blank string comparison with 0
本文介绍了SQL Server 2012空白字符串比较为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在SQL Server 2012上运行此查询:
I am running this query on SQL Server 2012:
select 'weird'
where '' = 0
正在返回'奇怪'
.
据我了解,''
与 0
完全不同.所以请解释为什么会发生这种情况.
As far as I understand, ''
is quite different from 0
. So please explain why the above happens.
谢谢
推荐答案
因此,来看看它们在 WHERE
子句
So, taking a look at the data types where they stand in the WHERE
clause
SELECT SQL_VARIANT_PROPERTY(0, 'BaseType'),SQL_VARIANT_PROPERTY('', 'BaseType')
它们分别返回 int,varchar
.
在比较两种不同的数据类型时,按照 MSDN .
When comparing two different data types, the data type with the lower precedence will convert to the higher precedence, per MSDN.
在这种情况下, Varchar
转换为 int
.
In this case, Varchar
converts to int
.
select cast('' AS int)
上面返回 0
.
因此
select 'weird' where 0 = 0
这篇关于SQL Server 2012空白字符串比较为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文