SQL Tutorials
8 min read

SQL Zero If NULL: Use ISNULL and COALESCE Without Hiding Missing Data

Build a billing report that separates a genuine zero from a charge that has not been calculated.

D

Dan Griffin

Senior Data Analyst · October 26, 2026

SQL Zero If NULL: Use ISNULL and COALESCE Without Hiding Missing Data
In this article: Learn how to replace NULL with zero in SQL Server using ISNULL and COALESCE, when to preserve missing values, and how zero-filling changes averages. Practice with a small SaaS billing dataset and a reporting challenge.

A billing dashboard shows every customer with a complete invoice amount. Two customers are still waiting for their usage charges to be calculated, but the report has quietly replaced those missing charges with zero.

The query runs successfully. The report looks finished. Finance could still send incomplete invoices.

If you searched for "sql zero if null," the basic T-SQL expressions are ISNULL(Amount, 0) and COALESCE(Amount, 0). The important decision comes before either expression: what does this NULL mean?

In this tutorial, you will build a report that replaces NULL where zero is justified and preserves it where the amount remains unknown.


The business rule comes before the function

Imagine a SaaS company preparing monthly invoices. Each row represents one customer invoice for the same billing month. All amounts are USD and exclude tax.

The source system has two different conventions:

  • DiscountAmount = NULL means no discount applies. The discount assessment is complete.
  • UsageCharge = NULL means the charge has not been calculated.
  • UsageCharge = 0.00 means usage was assessed and no extra charge applies.


These meanings belong to this source system. SQL cannot infer them from the column names. In another system, a missing discount might also be unknown.

For this exercise, the invoice amount is:

Subscription fee + usage charge - discount

An invoice is ready only when its usage charge is known.


Create the practice dataset

Paste this setup into an SSMS query window. Append the example query you want to test and execute both together as one batch. Do not put GO between them: the table variable belongs to its batch.

The setup creates sample data only in a table variable; it does not modify your application tables.


DECLARE @InvoiceDrafts TABLE
(
    InvoiceId       int           NOT NULL PRIMARY KEY,
    CustomerName    varchar(40)   NOT NULL,
    PlanName        varchar(20)   NOT NULL,
    SubscriptionFee decimal(10,2) NOT NULL,
    DiscountAmount  decimal(10,2) NULL,
    UsageCharge     decimal(10,2) NULL
);

INSERT INTO @InvoiceDrafts
    (InvoiceId, CustomerName, PlanName,
     SubscriptionFee, DiscountAmount, UsageCharge)
VALUES
    (101, 'Cedar Studio',  'Standard',   100.00, NULL,  0.00),
    (102, 'Harbor Media',  'Standard',   120.00, 30.00, 30.00),
    (103, 'Maple Design',  'Standard',    80.00, 10.00, 0.00),
    (104, 'Summit Labs',   'Enterprise', 200.00, NULL,  NULL),
    (105, 'Orchard Cloud', 'Enterprise', 200.00, 20.00, NULL);


Replace NULL with zero using ISNULL or COALESCE

For the discount column, either expression implements the agreed rule:


SELECT
    InvoiceId,
    DiscountAmount,
    ISNULL(DiscountAmount, 0) AS DiscountWithIsNull,
    COALESCE(DiscountAmount, 0) AS DiscountWithCoalesce
FROM @InvoiceDrafts
ORDER BY InvoiceId;


Both produce discounts of 0.00, 30.00, 10.00, 0.00 and 20.00, respectively.

ISNULL returns its replacement when the first expression is NULL. COALESCE returns the first non-NULL expression from its argument list. Their output values match here, but their type rules differ.

[1][2]



For example, COALESCE can express "use the agreed amount, otherwise the estimated amount, otherwise zero." That fallback still needs business approval: an estimate should not silently become a final charge.

With typed decimal columns like ours, replacing a NULL discount with integer literal 0 is valid. If your fallback contains fractions or uses a different type, choose the intended decimal precision and scale explicitly. ISNULL converts its replacement to the first expression's type; do not assume a decimal fallback changes an integer result into a decimal. [1][2]

Keep Reading

Get free access to the full article

Join data professionals reading weekly SQL tips, career guides, and interview prep — completely free.

No credit card. No spam. Unsubscribe anytime.

Recommended Reading