How to replace NULL with a different value in a query result in SQL Server

When you select rows from a table, the results may contain NULL values:

USE AdventureWorks
SELECT Name, Weight
FROM Production.Product

weight_value_null

If you want to replace the NULL values with some other value in query results, use ISNULL System Function that replaces NULL with a specified value:

USE AdventureWorks
SELECT Name, ISNULL (Weight, 0) AS Weight
FROM Production.Product

weight_value_null_replaced

Type of the replacement value is converted to the type of original replaced value, if the types are different.
If the conversion fails, you will get an error similar to this:

Msg 8114, Level 16, State 5, Line 2
Error converting data type varchar to numeric.

weight_value_null_replaced_conversion_error

The replacement value is truncated if it is longer than the original value.

If the post helped you, please share it:
Pin It

1 comment to How to replace NULL with a different value in a query result in SQL Server

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">