Why is T-SQL ISNULL() truncating the string and COALESCE is not?

后端 未结 3 1934
渐次进展
渐次进展 2020-12-10 10:34

Given the following:

SELECT ISNULL(\'XY\' + NULL, \'ABCDEFGHIJ\') -- Outputs ABC (Why?)
SELECT COALESCE(\'XY\' + NULL, \'ABCDEFGHIJ\') -- Outputs ABCDEFGHIJ
         


        
3条回答
  •  长情又很酷
    2020-12-10 11:14

    ISNULL() converts the replacement value to the type of the check expression. In this case, the type of the check expression is CHAR(2), so converting the replacement value truncates it (are you sure you're getting ABC and not just AB?).

    From the Microsoft documentation:

    replacement_value can be truncated if replacement_value is longer than check_expression.

提交回复
热议问题