Announcing our $3.4M seed round 🚀 Read more →

How to Use Coalesce in SQL Server in SQL Server

Imagine you're looking at a SQL Server integer column where some rows are null:

select day, tickets
from stats;
    day     | tickets 
------------+---------
 2020-02-01 | 1       
 2020-02-02 | null    
 2020-02-03 | 3       

Instead of having that null, you might want that row to be 0. To do that, use the coalesce() function, which returns the first non-null argument it's passed:

select day, coalesce(tickets, 0)
from stats;
    day     | tickets 
------------+---------
 2020-02-01 | 1       
 2020-02-02 | 0       
 2020-02-03 | 3       

Ready for a modern SQL editor?