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
๐ŸŒ
TablePlus
tableplus.com โ€บ blog โ€บ 2019 โ€บ 09 โ€บ sql-if-null-then-0.html
SQL IF NULL THEN 0 | TablePlus
September 11, 2019 - COALESCE โ€ฆ statement to return 0 as the alternative value when bonus value is NULL: SELECT emp_no, salary, from_date, to_date, COALESCE(bonus, 0) FROM salaries; In MySQL you can also use IFNULL function to return 0 as the alternative for the NULL values: SELECT emp_no, salary, from_date, ...
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.

๐ŸŒ
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?

๐ŸŒ
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 - The IFNULL function checks if the lab_hours column is NULL and replaces it with 0 before calculating the average. Similar to MySQL, SQL Server offers a dedicated function, ISNULL, to replace NULL values with a specified default. It has the same syntax as the IFNULL function of MySQL: ... This replaces the NULL values in the result with 0 and then ...
๐ŸŒ
Reddit
reddit.com โ€บ r/sql โ€บ will column >0 also exclude rows were column is null?
r/SQL on Reddit: Will column >0 also exclude rows were column is null?
February 6, 2024 -

Sorry for the basic question but if I want to create a case statement on a column that can have values that are 0 or null, would

Case When column = 0

work or would I always have to also include "or column is null"

Find elsewhere
๐ŸŒ
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.
๐ŸŒ
W3Schools
w3schools.com โ€บ sql โ€บ sql_isnull.asp
SQL IFNULL(), ISNULL(), COALESCE(), and NVL() Functions
SQL Examples SQL Editor SQL Quiz SQL Exercises SQL Server SQL Syllabus SQL Study Plan SQL Bootcamp SQL Certificate SQL Training ... Suppose that the "UnitsOnOrder" column is optional, and may contain NULL values. ... In the example above, if any of the "UnitsOnOrder" values are NULL, the result will be NULL. ... The MS Access IsNull() function returns TRUE (-1) if the expression is a null value, otherwise FALSE (0):
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ how-can-i-return-0-for-null-in-mysql
How can I return 0 for NULL in MySQL?
The following is the output that returns 0 for every NULL. +---------------+ | IFNULL(id, 0) | +---------------+ | 0 | | 123 | | 442 | | 333 | | 0 | | 0 | +---------------+ 6 rows in set (0.00 sec)
๐ŸŒ
w3resource
w3resource.com โ€บ mysql โ€บ control-flow-functions โ€บ if-null-function.php
MySQL IFNULL() function - w3resource
The IFNULL() function evaluates the first argument (0). Since 0 is not NULL, the function returns this value, ignoring the second argument (2). Hence, the query returns 0 as the output.
๐ŸŒ
SQLServerCentral
sqlservercentral.com โ€บ forums โ€บ topic โ€บ nullif-and-0
NULLIF and 0 โ€“ SQLServerCentral Forums
March 25, 2021 - Second - NULLIF has another valuable (IMO) application. If you want to have NULLs everywhere when you have "non-defined" value (as empty string or 0) - NULLIF(<something>, '') will do the job.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ answers โ€บ questions โ€บ 586124 โ€บ show-0-instead-of-null-in-dynamic-sql-stored-proc
Show 0 Instead of Null In Dynamic SQL Stored Proc - Microsoft Q&A
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. 1 comment Show comments for this answer Report a concern ... . . . declare @cols1 nvarchar(max) = 'ordernumber' select @cols1 = @cols1 + ', isnull(' + QUOTENAME(vendorsku) + ',0) as ' + QUOTENAME(vendorsku) FROM (select distinct vendorsku from ProdInfo) as tmp SET @SQL_QUERY = N'SELECT ' + @cols1 + ' from .
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ sql โ€บ t-sql โ€บ language-elements โ€บ nullif-transact-sql
NULLIF (Transact-SQL) - SQL Server | Microsoft Learn
Applies to: SQL Server Azure SQL ... Fabric SQL database in Microsoft Fabric Preview ยท Returns a null value if the two specified expressions are equal. For example, SELECT NULLIF(4,4) AS Same, NULLIF(5,7) AS Different; returns NULL for the first column (4 and 4) because ...
๐ŸŒ
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 - The COALESCE function returns the first non-null value in a list. The value can be anything ranging from text to numbers. For examples, check out w3 schools. However, in our context, if a customer has not made a purchase in the given time period, it should return a zero, hence the title.