Did you know that SQL Server’s IsNull() function does an implicit conversion?
1 | ISNULL ( check_expression , replacement_value) |
The documentation explicitly states that
The value of check_expression is returned if it is not NULL; otherwise, replacement_value is returned after it is implicitly converted to the type of check_expression, if the types are different. replacement_value can be truncated if replacement_value is longer than check_expression. (emphasis mine)
1 | DECLARE @TestIsNull table ( |
See the problem? Data type of EmpId column on line 2 is varchar(10) and the data type of empIdFilter is varchar(9).
Therefore, ISNULL will convert varchar(10) to varchar(9) resulting in truncation. Now imagine this in a stored procedure where the EmpId column datatype is not obvious and filter variable is declared somewhere far away from the SQL statement. It becomes a pretty insidious bug.
In order to avoid this, you can write your SQL statement like this.
1 | DECLARE @TestIsNull table ( |