Cloud Training (Online)




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

Senthil Kumar B
Senthil Kumar is a former Microsoft MVP (Most Valuable Professional). He is a Co-Author of the book "Windows 10 Development Recipes using JavaScript and CSS" for Apress Publication. He is a technical presenter, blogger, mentor and a Geek.  Senthil is a regular speaker is various local user groups. He has presented at conferences like Great Indian Developer Summit (GIDS) & Microsoft DevCamps. You can reach out to him via his Twitter handle @isenthil.