SQL Server ISNULL Function Explained
In SQL Server, ISNULL() Function is used to return an alternative value when a querying value is NULL:
Here is the general syntax:
ISNULL ( check_expression , replacement_value )
replacement_value
will be returned as the result of the query if check_expression returns a null value. It must have the same data type of check_expression
. If the check_expression
is a literal NULL, return the data type of the replacement_value
.
It’s similar to IFNULL()
or COALESCE()
functions that we can use in MySQL, or NVL()
function in Oracle.
Let’s look at an example of using ISNULL()
in SQL Server:
SELECT order_id, ISNULL(total_amount, 0) AS total_paid
FROM orders;
The query above is to return the list of all orders and the total amount of money each order. If the total amount is NULL, it will be listed as 0.
Or:
SELECT ISNULL(NULL, '2018-09-21');
The result is: '2018-09-21'
Need a good GUI Client to work with MS SQL Server? TablePlus provides a modern, native tool with intuitive UI to manage multiple relational databases in cluding SQL Server, MySQL, PostgreSQL, SQLite, Oracle…
And it’s available for free!
Not on Mac? Download TablePlus for Windows.
On Linux? Download TablePlus for Linux
Need a quick edit on the go? Download TablePlus for iOS.