SQL Server Documentation

Transact-SQL Reference

ISNULL (Transact-SQL)

Replaces a NULL value with a specified replacement value.

Syntax


ISNULL ( check_expression , replacement_value )
            

Arguments

check_expression

Is the expression to check for NULL values.

replacement_value

Is the expression to return if check_expression is NULL. replacement_value must be of a data type that is compatible with the data type of check_expression.

Return Type

Returns the same data type as check_expression.

Permissions

Any user can execute the ISNULL function.

Examples

A. Using ISNULL to replace NULL values in a query

The following example returns the value of the Bonus column from the Sales table. If the Bonus value is NULL, it returns 0.


USE AdventureWorks2019;
GO
SELECT JobTitle, ISNULL(Bonus, 0) AS Bonus
FROM HumanResources.Employee
WHERE JobTitle LIKE 'Production%';
GO
            

B. Using ISNULL with string concatenation

The following example returns the MiddleName or a space if MiddleName is NULL.


USE AdventureWorks2019;
GO
SELECT FirstName + ' ' + ISNULL(MiddleName, ' ') + ' ' + LastName AS Name
FROM Person.Person
WHERE LastName = 'Smith'
ORDER BY FirstName;
GO
            

C. Using ISNULL with implicit conversion

If check_expression is of a data type that cannot be implicitly converted to the data type of replacement_value, an error will be returned.


USE AdventureWorks2019;
GO
SELECT ISNULL(TerritoryID, 'N/A')
FROM Sales.SalesTerritory;
GO
            

Warning: The preceding statement will fail because the integer data type of TerritoryID cannot be implicitly converted to the character string data type of 'N/A'.

Remarks

See Also