SQL Server 2012 blank string comparison with 0

ε祈祈猫儿з 提交于 2020-02-05 07:06:22

问题


I am running this query on SQL Server 2012:

select 'weird' 
where '' = 0

It's returning 'weird'.

As far as I understand, '' is quite different from 0. So please explain why the above happens.

Thanks


回答1:


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')

They return int, varchar respectively.

When comparing two different data types, the data type with the lower precedence will convert to the higher precedence, per MSDN.

In this case, Varchar converts to int.

select cast('' AS int)

The above returns 0.

Thus

select 'weird' where 0 = 0


来源:https://stackoverflow.com/questions/25142303/sql-server-2012-blank-string-comparison-with-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!