Data types and expressions
COALESCE and NULLIF
Display missing values and intentionally turn matching values into NULL.
Lesson 22 of 84
Reading: about 2 min
Lesson + practice: about 12 min
Difficulty: Beginner
On this page4 sections
This lesson uses the unmodified DDI_SQL_Training dataset v1.0.0 on SQL Server 2019 or later. Display missing values and intentionally turn matching values into NULL.
Run the example
sql
SELECT TOP (5) CustomerID,
COALESCE(Email, N'[missing]') AS EmailDisplay
FROM dbo.Customers
ORDER BY CustomerID;Read it step by step
- Email is nullable in dbo.Customers.
- COALESCE returns Email when present and the fallback text when it is NULL.
- The original Email value is unchanged; ORDER BY fixes the five-row sample.
Check the result
| CustomerID | EmailDisplay |
|---|---|
| 1 | alex.bennett@example.invalid |
| 2 | maya.clark@example.invalid |
| 3 | leo.evans@example.invalid |
| 4 | isla.foster@example.invalid |
| 5 | [missing] |
The fallback is a display label, not a new email address stored in the table.
Practice
For Products 1 and 2, return NULL instead of ListPrice when the price equals 39.00.
Check your answer and expected result
Reveal answer
sql
SELECT ProductID, NULLIF(ListPrice, 39.00) AS PriceUnless39
FROM dbo.Products
WHERE ProductID IN (1, 2)
ORDER BY ProductID;| ProductID | PriceUnless39 |
|---|---|
| 1 | NULL |
| 2 | 129.00 |
Product 1 matches 39.00 and shows NULL; Product 2 keeps its price.
Your progress
Reveal the check answer or reach the end to complete this lesson.