Handling missing values
Let me start by creating and populating a small table with a couple of missing values in some cells, denoted by the reserved word NULL
in SQL Server:
USE AdventureWorksDW2017; GO DROP TABLE IF EXISTS dbo.NULLTest; GO CREATE TABLE dbo.NULLTest ( c1 INT NULL, c2 INT NULL, c3 INT NULL ); GO INSERT INTO dbo.NULLTest VALUES (1, NULL, 3), (4, 5, 6), (NULL, 8, 9), (10, 11, 12), (13, NULL, NULL); GO
The content of the table is as follows:
c1 c2 c3 ----------- ----------- ----------- 1 NULL 3 4 5 6 NULL 8 9 10 11 12 13 NULL NULL
In T-SQL, you can use the ISNULL()
and COALESCE()
functions to work with NULL
s. The first one has only two parameters and returns the value of the first parameter, if it is not NULL
, and the values of the second parameter otherwise. The second function, just mentioned, accepts multiple parameters and returns the first one that is not NULL
. You can...