The SQL ISNULL
function can be used to replace null values in the results of a query with alternate data. The ISNULL
function is used by Microsoft SQL. Other SQL platforms, such as Oracle and MySQL use comparable functions such as NVL
, IFNULL
and COALESCE
.
Employees Table
employeeID | employeeName | age |
---|---|---|
1000 | John Smith | 40 |
1001 | Fred White | 27 |
1002 | 53 | |
1003 | Samuel Williams | 31 |
In this example, when we query the employees table, we want to replace null values in the employeeName
field with ‘Unknown’.
Syntax
SELECT ISNULL(column_name,'value')
FROM Table
Example
SELECT ISNULL(employeeName,'Unknown') as [Employee Name]
FROM employees
Results
Employee Name |
---|
John Smith |
Fred White |
Unknown |
Samuel Williams |
The SQL ISNULL
function can be very helpful when null values need to be replaced in the result set with alternate information.