Noob Noob - 3 months ago 5
SQL Question

What does IsNull in SQL Server procedure do?

I have following update statement already written in a SQL Server stored procedure.

What does

do here?

Update [dbo].[Images]
set ImageName = IsNull(@ImageName, ImageName), ItemId = IsNull(@ItemId, ItemId)
where TypeId = @TypeId;

I am fairly new to database stuff.


It is similar to COALESCE in that it returns the value of the second parameter if the first is NULL.

You have to be careful with ISNULL in certain situations because it may truncate the second parameter value if the type of the first is defined as smaller. For instance:

DECLARE @val1 CHAR(1);
DECLARE @val2 CHAR(2) = 'AB';

SELECT ISNULL(@val1,@val2); -- Returns 'A', not 'AB'
SELECT COALESCE(@val1,@val2); -- Returns 'AB'