ISNULL ( check_expression , replacement_value )
Arguments
check_expression
The expression to be checked for NULL
. check_expression can be of any type.
replacement_value
The expression to be returned if check_expression is NULL
. replacement_value must be of a type that is implicitly convertible to the type of check_expression.
Return types
Returns the same type as check_expression. If a literal NULL
is provided as check_expression, ISNULL
returns the data type of the replacement_value. If a literal NULL
is provided as check_expression and no replacement_value is provided, ISNULL
returns an int.
The value of check_expression is returned if it's not NULL
. Otherwise, replacement_value is returned after it's 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.
Use COALESCE to return the first non-null value.
Examples
The code samples in this article use the AdventureWorks2022
or AdventureWorksDW2022
sample database, which you can download from the Microsoft SQL Server Samples and Community Projects home page.
A. Use ISNULL with AVG
The following example finds the average of the weight of all products. It substitutes the value 50
for all NULL
entries in the Weight
column of the Product
table.
USE AdventureWorks2022;
SELECT AVG(ISNULL(Weight, 50))
FROM Production.Product;
Here's the result set.
59.79
B. Use ISNULL
The following example selects the description, discount percentage, minimum quantity, and maximum quantity for all special offers in AdventureWorks2022
. If the maximum quantity for a particular special offer is NULL
, the MaxQty
shown in the result set is 0.00
.
USE AdventureWorks2022;
SELECT Description, DiscountPct, MinQty, ISNULL(MaxQty, 0.00) AS 'Max Quantity'
FROM Sales.SpecialOffer;
Here's the result set.
Description
DiscountPct
MinQty
Max Quantity
The following example uses ISNULL
to replace a NULL
value for Color
, with the string None
.
USE AdventureWorks2022;
SELECT ProductID,
Name,
ProductNumber,
ISNULL(Color, 'None') AS Color
FROM Production.Product;
Here's a partial result set.
ProductID
ProductNumber
Color
C. Test for NULL
in a WHERE clause
Don't use ISNULL
to find NULL
values. Use IS NULL
instead. The following example finds all products that have NULL
in the weight column. Note the space between IS
and NULL
.
USE AdventureWorks2022;
SELECT Name, Weight
FROM Production.Product
WHERE Weight IS NULL;
Examples: Azure Synapse Analytics and Analytics Platform System (PDW)
D. Use ISNULL with AVG
The following example finds the average of the weight of all products in a sample table. It substitutes the value 50
for all NULL
entries in the Weight
column of the Product
table.
-- Uses AdventureWorksDW
SELECT AVG(ISNULL(Weight, 50))
FROM dbo.DimProduct;
Here's the result set.
52.88
E. Use ISNULL
The following example uses ISNULL
to test for NULL
values in the column MinPaymentAmount
and display the value 0.00
for those rows.
-- Uses AdventureWorks
SELECT ResellerName,
ISNULL(MinPaymentAmount,0) AS MinimumPayment
FROM dbo.DimReseller
ORDER BY ResellerName;
Here's a partial result set.
ResellerName
MinimumPayment
F. Use IS NULL to test for NULL in a WHERE clause
The following example finds all products that have NULL
in the Weight
column. Note the space between IS
and NULL
.
-- Uses AdventureWorksDW
SELECT EnglishProductName, Weight
FROM dbo.DimProduct
WHERE Weight IS NULL;
Related content
Expressions (Transact-SQL)
IS NULL (Transact-SQL)
System Functions by category for Transact-SQL
WHERE (Transact-SQL)
COALESCE (Transact-SQL)