SQL Server NULL整数使用ISNULL清空字符串

时间:2022-12-08 15:45:12

My curiosity always gets the best of me and I've searched online for an explanation to this and came up with nothing (could be because I didn't use the right terms.)

我的好奇心始终是我最好的,我在网上搜索了一个解释,并没有得到任何结果(可能是因为我没有使用正确的条款。)

Can someone please explain why SQL Server returns a value of zero (0) when the following is executed, instead of an empty string ('').

有人可以解释为什么SQL Server在执行以下操作时返回值为零(0),而不是空字符串('')。

    DECLARE @I AS INT
    SET @I = NULL
    SELECT ISNULL(@I, '') -- 0

2 个解决方案

#1


2  

As declared here, the second argument to ISNULL is the replacement_value, which "must be of a type that is implicitly convertible to the type of check_expresssion." Implicitly converting '' to INT results in 0.

正如这里声明的那样,ISNULL的第二个参数是replacement_value,它“必须是一个可以隐式转换为check_expresssion类型的类型”。将''隐式转换为INT会导致0。

#2


2  

Because @I is declared as an INT, the empty string is implicitly CAST as an integer resulting in a ZERO.

因为@I被声明为INT,所以空字符串隐式地CAST为整数,从而产生ZERO。

#1


2  

As declared here, the second argument to ISNULL is the replacement_value, which "must be of a type that is implicitly convertible to the type of check_expresssion." Implicitly converting '' to INT results in 0.

正如这里声明的那样,ISNULL的第二个参数是replacement_value,它“必须是一个可以隐式转换为check_expresssion类型的类型”。将''隐式转换为INT会导致0。

#2


2  

Because @I is declared as an INT, the empty string is implicitly CAST as an integer resulting in a ZERO.

因为@I被声明为INT,所以空字符串隐式地CAST为整数,从而产生ZERO。