SQL Server NULL Integer to Empty String using ISNULL

Ahz picture Ahz · Aug 19, 2014 · Viewed 9k times · Source

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

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

Answer

hunch_hunch picture hunch_hunch · Aug 19, 2014

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.