Use the ISNULL function in MSSQL to define default values in your queries

When I work with SQL-Views and create complex SELECT statements that return data from multiple tables, I like to define default values in case a row contains columns with NULL as value. To do this I use the ISNULL function.

Lets check the logic of the function with an example:

SELECT ISNULL(FirstName, 'Christos') AS Name
FROM Customers;

The first parameter of the function is the column to be checked for NULL. The second parameter is the default value when NULL.

In the previous example the query returns a column named Name with the contents of the FirstName column of the Customers table. When the value of FirstName is null, then the value Christos is returned instead.

Simple as that…

comments powered by Disqus