When you want to replace a possibly null column with something else, use IsNull.

SELECT ISNULL(myColumn, 0 ) FROM myTable

This will put a 0 in myColumn if it is null in the first place.

Answer from phadaphunk on Stack Overflow
Top answer
1 of 13
552

When you want to replace a possibly null column with something else, use IsNull.

SELECT ISNULL(myColumn, 0 ) FROM myTable

This will put a 0 in myColumn if it is null in the first place.

2 of 13
118

You can use both of these methods but there are differences:

SELECT ISNULL(col1, 0 ) FROM table1
SELECT COALESCE(col1, 0 ) FROM table1

Comparing COALESCE() and ISNULL():

  1. The ISNULL function and the COALESCE expression have a similar purpose but can behave differently.

  2. Because ISNULL is a function, it is evaluated only once. As described above, the input values for the COALESCE expression can be evaluated multiple times.

  3. Data type determination of the resulting expression is different. ISNULL uses the data type of the first parameter, COALESCE follows the CASE expression rules and returns the data type of value with the highest precedence.

  4. The NULLability of the result expression is different for ISNULL and COALESCE. The ISNULL return value is always considered NOT NULLable (assuming the return value is a non-nullable one) whereas COALESCE with non-null parameters is considered to be NULL. So the expressions ISNULL(NULL, 1) and COALESCE(NULL, 1) although equivalent have different nullability values. This makes a difference if you are using these expressions in computed columns, creating key constraints or making the return value of a scalar UDF deterministic so that it can be indexed as shown in the following example.

-- This statement fails because the PRIMARY KEY cannot accept NULL values -- and the nullability of the COALESCE expression for col2 -- evaluates to NULL.

CREATE TABLE #Demo 
( 
    col1 integer NULL, 
    col2 AS COALESCE(col1, 0) PRIMARY KEY, 
    col3 AS ISNULL(col1, 0) 
); 

-- This statement succeeds because the nullability of the -- ISNULL function evaluates AS NOT NULL.

CREATE TABLE #Demo 
( 
    col1 integer NULL, 
    col2 AS COALESCE(col1, 0), 
    col3 AS ISNULL(col1, 0) PRIMARY KEY 
);
  1. Validations for ISNULL and COALESCE are also different. For example, a NULL value for ISNULL is converted to int whereas for COALESCE, you must provide a data type.

  2. ISNULL takes only 2 parameters whereas COALESCE takes a variable number of parameters.

    if you need to know more here is the full document from msdn.

๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ sql basics โ€บ how to replace null with 0 in sql
How to Replace NULL With 0 in SQL Baeldung on SQL
January 27, 2025 - We can use the COALESCE function to replace NULL values with 0. The COALESCE function is a standard SQL function that finds and returns the first non-NULL value from its argument list.
๐ŸŒ
Reddit
reddit.com โ€บ r/sql โ€บ replace null count with a zero (0)
r/SQL on Reddit: Replace null count with a zero (0)
December 22, 2022 -

Hello,

This is my query

SELECT ordertype, 
status, 
sum (COUNT (printdate)) over (), 
date(char(1900000+requestdate)), 
businessunit 
FROM Casepallet

WHERE date(char(1900000+requestdate)) IN (current date, current date + 1 days) 
AND business unit='         SCS' 
AND ordertype!='T1' 
AND status = 'X'

group by ordertype, business unit, request date, status, printdate

order by printdate desc limit 1

Sometimes the sum/count of printdate returns nothing, it's null. If it's null I want it to return 0.

I tried doing it like this: COALESCE (sum (COUNT (printdate)) over (), 0), but it doesn't work, it still returns null.

Can anyone guide me here on where I go wrong?

๐ŸŒ
Atlassian
atlassian.com โ€บ data โ€บ sql โ€บ how-to-replace-nulls-with-0s
Null Replacements in SQL | Atlassian
Null Values can be replaced in SQL by using UPDATE, SET, and WHERE to search a column in a table for nulls and replace them. In the example above it replaces them with 0.
๐ŸŒ
SQL Team
sqlteam.com โ€บ forums โ€บ topic.asp
Replace NULLs with 0 in tables - SQL Server Forums
Microsoft SQL Server articles, forums and blogs for database administrators (DBA) and developers.
Find elsewhere
๐ŸŒ
Medium
medium.com โ€บ @mwaurahmn โ€บ lesson-2-replacing-null-values-with-0-in-sql-8646b887d0ad
Lesson 2: Replacing Null values with 0 in SQL | by Martha Mwaura | Medium
August 4, 2023 - Using dbt, the goal is to transform the data make it ready for use by the Business Intelligence team. Therefore, the NULLs have got to go! The big question: How do you replace NULL values in SQL?
๐ŸŒ
Quora
quora.com โ€บ How-do-you-replace-null-values-with-zeros-in-SQL
How to replace null values with zeros in SQL - Quora
Answer (1 of 3): Use the COALESCE(column, 0) function. The SALARY below can be NULL and the COALESCE chooses the first non-NULL value from left to right. [code]SELECT EMP_ID, COALESCE(SALARY, 0) FROM EMPLOYEE WHERE DEPT_ID = 23; [/code]
๐ŸŒ
TablePlus
tableplus.com โ€บ blog โ€บ 2019 โ€บ 09 โ€บ sql-if-null-then-0.html
SQL IF NULL THEN 0 | TablePlus
September 11, 2019 - When selecting data from a table, there might be some NULL values that you donโ€™t want to show, or you want to replace it with 0 for the aggregate functions. Then you can use COALESCE to replace the NULL with 0. For example, we have the table salaries with 5 columns: emp_no, from_date, to_date, ...
๐ŸŒ
SQLines
sqlines.com โ€บ teradata โ€บ functions โ€บ zeroifnull
Teradata - ZEROIFNULL Function - Replace NULL Values with 0 - SQLines Tools
ZEROIFNULL function replaces NULL values with 0. Quick Example: SELECT ZEROIFNULL(NULL); -- Result: 0 ZEROIFNULL Overview Summary information: Syntax ZEROIFNULL(expression) Alternatives CASE WHEN expression IS NULL THEN 0 ELSE expression END CASE is ANSI SQL compliant Related Functions:
๐ŸŒ
Quora
quora.com โ€บ How-do-you-replace-null-values-with-0-in-SQL
How to replace null values with 0 in SQL - Quora
Answer (1 of 5): The simplest thing would be in a stored procedure. When you are defining the parameters you would just add โ€œ= 0โ€. This will be the default value if nothing or null is sent to the procedure.
๐ŸŒ
Reddit
reddit.com โ€บ r/sql โ€บ replacing null with 0?
r/SQL on Reddit: Replacing NULL with 0?
November 10, 2021 -

I have 2 datasets, one is called Company Locations, with the columns Company ID and Location, and the second is called Company Headcount, with the columns Company ID and Headcount.

SELECT *
  FROM company_locations AS cl
LEFT JOIN company_headcounts AS ch
    ON cl.company_id = ch.company_id;

I am joining these 2 datasets, and there are empty values under Headcount because Company Locations has company IDs that donโ€™t appear in Company Headcount.

How can I replace these empty values with 0s instead?

๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ replace-0-with-null-in-mysql
Replace 0 with null in MySQL?
SELECT *,NULLIF(yourColumnName,0) as anyVariableName from yourTableName; To understand the above syntax, let us create a table. The query to create a table is as follows โˆ’ ยท mysql> create table Replace0WithNULLDemo -> ( -> Id int NOT NULL auto_increment, -> Name varchar(20), -> Marks int, ...
๐ŸŒ
My Tec Bits
mytecbits.com โ€บ home โ€บ microsoft โ€บ sql server โ€บ how to replace null with 0 in sql server?
How to replace NULL with 0 in SQL Server? | My Tec Bits
November 4, 2023 - DECLARE @Int_Val INT; SET @Int_Val = NULL; SELECT ISNULL(@Int_Val, 0) AS Result; ISNULL can be used to replace NULL with any value. The next method is by using the COALESCE function.
๐ŸŒ
SQLServerCentral
sqlservercentral.com โ€บ forums โ€บ topic โ€บ replace-zero-with-null-values
Replace zero with null values โ€“ SQLServerCentral Forums
July 22, 2008 - If the value is NULL you can use ISNULL. If it is blank spaces you can use the Case statement. ... Hey thanks Ken. This query works fine but how do I modify this query to update the entire table? ... Read up on ISNULL() in books online, Ken pointed you in the right direction.
๐ŸŒ
C# Corner
c-sharpcorner.com โ€บ UploadFile โ€บ 219d4d โ€บ different-ways-to-replace-null-in-sql-server
Replace Nulls With Specified Values in SQL Server
February 21, 2023 - The ISNULL Function is a built-in function to replace nulls with specified replacement values. To use this function, you only need to pass the column name in the first and second parameters and pass the value with which you want to replace the ...
๐ŸŒ
Intellipaat
intellipaat.com โ€บ home โ€บ blog โ€บ how to replace null values with โ€˜0โ€™ in sql
How to replace NULL values with '0' in SQL
May 31, 2025 - Learn how to replace NULL values with '0' in SQL using various techniques, including the ISNULL() function and COALESCE() function