Introduction
In this tutorial, I am going to explain various methods to handle NULLs in SQL Server. This detailed article will cover the following topics as follows:
- Introduction
- What exactly is NULL?
- Different Ways to handle NULLs
- Difference between ISNULL and COALESCE Function
- Conclusion
What Exactly Is NULL?
A NULL is a term with a value other than 0 or an empty string, i.e. an unknown value that data is missing.
As per Wikipedia, "Null or NULL is a special marker used in Structured Query Language to indicate that a data value does not exist in the database. Introduced by the creator of the relational database model, E. F. Codd, SQL Null serves to fulfill the requirement that all true relational database management systems (RDMS) support a representation of "missing information and inapplicable information". Codd also introduced the use of the lowercase Greek omega (ω) symbol to represent Null in database theory. A null should not be confused with a value of 0. A null value indicates a lack of a value, which is not the same thing as a value of zero. SQL null is a state, not a value."
First, let's create a database with a table containing some dummy data. Here, I am providing you with the database along with the table containing the records, on which I am showing you the various examples. Let's see.
CREATE TABLE HandleISNULL(
EmployeeID INT IDENTITY (31100,11),
EmployerID BIGINT NOT NULL DEFAULT 228866,
EmployeeFullName VARCHAR (100) NOT NULL,
DefaultTask VARCHAR(100),
Salary BIGINT,
PrimaryContactNo BIGINT,
SecondaryContactNo BIGINT,
Telephone INT
PRIMARY KEY (EmployeeID)
)
Now, insert some dummy data in it.
INSERT INTO HandleISNULL VALUES
(DEFAULT, 'Prabhas', 'Development', 2100000, 789654123, 741258963, NULL),
(DEFAULT, 'John Sinha', NULL, NULL, 123654789, NULL, NULL),
(DEFAULT, 'Meenu Tiwari', 'HR', 1800000, 456987132, 963258741, NULL),
(DEFAULT, 'Neeraj__Chopra', NULL, NULL, 951478632, NULL, NULL),
(DEFAULT, 'Neeraj Chopra', 'Marketing', 900000, 485936217, NULL, NULL),
(DEFAULT, 'Onkar Sharma', 'Admin', 5200000,369147852, 326159874, NULL),
(DEFAULT, 'Bajrang Punia', 'HR', 1700000, 98653247, 784512369, NULL),
(DEFAULT, 'Satish', 'Development', 2300000, NULL, NULL, NULL),
(DEFAULT, 'Parneeti Dhoppra', 'Support', 700000, 123654789, 48963157, NULL),
(DEFAULT, 'Ananya Pandey', NULL, 300000, 547896321, NULL,NULL),
(DEFAULT, 'Shraddha Kapoor', 'Account', 1500000, NULL, NULL, 265819),
(DEFAULT, 'Akshay Sharma', NULL, NULL, 648521789, NULL,NULL)
Let's check our following table by using the following query.
SELECT * FROM OnkarSharma..HandleISNULL

Note
Here, we can see that symbols “=” and “!=” do not work with “NULL” values as follows.
-- Wrong Query
SELECT * FROM OnkarSharma..HandleISNULL
WHERE DefaultTask = NULL
SELECT * FROM OnkarSharma..HandleISNULL
WHERE DefaultTask != NULL

-- Right Query
SELECT * FROM OnkarSharma..HandleISNULL
WHERE DefaultTask IS NULL
SELECT * FROM OnkarSharma..HandleISNULL
WHERE DefaultTask IS NOT NULL

ISNULL() Function
The ISNULL() returns a specified replacement value if the expression is NULL. It replaces NULL with the specified replacement value if the expression is NULL.
Key Points
- ISNULL() takes only two parameters.
- ISNULL() is Microsoft SQL Server-specific.
- The ISNULL() return value is always considered NON-NULLable.
- The first parameter's data type determines the data type of the value returned by ISNULL().
Syntax
ISNULL(Expression, ReplacementValue);
Examples
The examples in this section demonstrate the functionality of the ISNULL() function. Let's see.
Simple Example
SELECT ISNULL (NULL, 108) NULL_REPLACEMENT;
SELECT ISNULL (NULL,'SQLServer') NULL_REPLACEMENT;
SELECT ISNULL ('OnkarSharma','SQLServer') NULL_REPLACEMENT;
SELECT ISNULL (NULL,GETDATE()) NULL_REPLACEMENT;

Complex Example
Replace the NULL with custom values/messages.
The following example returns the list of all employees who did not provide an alternate phone/contact number.
SELECT
EmployerID,
EmployeeID,
EmployeeFullName,
ISNULL(DefaultTask, 'Trainee') AS DefaultTask, PrimaryContactNo,
ISNULL(CAST(SecondaryContactNo AS VARCHAR), 'NOT Available') AS 'Secondary Contact No'
FROM OnkarSharma..HandleISNULL
WHERE SecondaryContactNo IS NULL





Join the conversation! Your thoughts help the community grow.