Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server NULL Integer to Empty String using ISNULL

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
like image 779
Ahz Avatar asked Oct 01 '22 01:10

Ahz


2 Answers

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.

like image 182
hunch_hunch Avatar answered Oct 25 '22 14:10

hunch_hunch


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

like image 2
T McKeown Avatar answered Oct 25 '22 12:10

T McKeown