How to replace NULL values with alternate values in SQL Server ?

Assume a scenario where you are trying to get the records from the table where some of the records contains NULL column values and you would like to replace it with an alternate value.

How to replace NULL values with alternate values in SQL Server ?

Use ISNULL method which accepts two parameters
– column name (value)
– alternate value if the value is NULL

The ISNULL method simply checks if the specified expression is NULL and if this is true it replaces the NULL value with the alternate value. The return type value (second parameter) should be of the same type as the first parameter.

Here’s an example of the usage of the ISNULL method.

SELECT JobTitle,
OrganizationLevel = ISNULL(CAST(OrganizationLevel as varchar(200)),'-- No Top Level Found--')
FROM HumanResources.Employee

image

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this blog post, let’s learn about the error message “1459 – An error occurred while accessing the database mirroring...
In this blog post, let’s learn about the error message “7937 – Columnstore index has one or more missing column...