选择isnull时的情况(' sdas',0)= 0然后' hi'别的' bye'结束为'价值'

时间:2014-10-15 08:52:25

标签: sql sql-server tsql

以下查询

select case when isnull('23',0)=0 then 'hi' else 'bye' end as 'Value'

返回bye

BUT

select case when isnull('sdas',0)=0 then 'hi' else 'bye' end as 'Value'

在MS-SqlServer2008R2中返回以下错误消息

Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value 'sdas' to data type int.

你能澄清它在哪个阶段完成转换

2 个答案:

答案 0 :(得分:1)

'23'可以动态转换为int,但'sdas'不能。 对所有变量采用string(varchar)-type:

select case when isnull('sdas','0')='0' then 'hi' else 'bye' end as 'Value'

答案 1 :(得分:0)

问题是将sdas转换为整数0。您可以使用isnull运算符

,而不是使用is null函数
select case when 'sdas' is null then 'hi' else 'bye' end as 'Value'