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.

🌐
W3Schools
w3schools.com › sql › sql_isnull.asp
SQL IFNULL(), ISNULL(), COALESCE(), and NVL() Functions
SELECT ProductName, UnitPrice * (UnitsInStock + IIF(IsNull(UnitsOnOrder), 0, UnitsOnOrder)) FROM Products; ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: [email protected] · ...
🌐
TablePlus
tableplus.com › blog › 2019 › 09 › sql-if-null-then-0.html
SQL IF NULL THEN 0 | TablePlus
September 11, 2019 - 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, salary, bonus. But the bonus column is optional and may contain NULL values. Run this SELECT … COALESCE … statement to return 0 as the alternative value ...
🌐
SQLServerCentral
sqlservercentral.com › forums › topic › if-null-then-zero-0
IF NULL then Zero (0) – SQLServerCentral Forums
July 28, 2010 - To simplify even more: SELECT ActiveProd = COUNT( CASE WHEN StatusID = 1 THEN ProdID END), InactiveProd = COUNT( CASE WHEN StatusID = 2 THEN ProdID END), DiscontinutedProd = COUNT( CASE WHEN StatusID = 3 THEN ProdID END), ProdCountDT = RIGHT( CONVERT(varchar(11),DATEADD(MM, DATEDIFF(MM, 0, CreatedDate),0), 106),8) FROM #Table_Temp TT --WHERE dateadd(month, datediff(month, 0, CreatedDate),0) BETWEEN getdate() - 365 and getdate() GROUP BY DATEDIFF(MM, 0, CreatedDate) ORDER BY ProdCountDT;
🌐
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?

🌐
Snowflake Documentation
docs.snowflake.com › en › sql-reference › functions › zeroifnull
ZEROIFNULL | Snowflake Documentation
If the value of the input expressions ... and ‘s’ (scale) depend upon the input expression. For example, if the input expression is 3.14159, then the data type of the output value will be NUMBER(7, 5)....
Find elsewhere
🌐
SQLServerCentral
sqlservercentral.com › forums › topic › nullif-and-0
NULLIF and 0 – SQLServerCentral Forums
March 25, 2021 - CAST('0' AS int) and CAST('' AS int) both return 0, so the 4 expressions are equivalent to: ... The NULLIF function returns NULL if the 2 arguments are equal, otherwise it just returns the first argument. The first and third expressions are clearly equivalent expressions, so return NULL.
🌐
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.
🌐
Modern SQL
modern-sql.com › concept › null
Modern SQL: NULL — purpose, comparisons, NULL in expressions, mapping to/from NULL
The effect of nulls (first|last) can be obtained with a case expression in all databases. The following example implements order by c nulls first: ORDER BY CASE WHEN c IS NULL THEN 0 ELSE 1 END , c
🌐
W3Schools
w3schools.com › mysql › mysql_ifnull.asp
MySQL IFNULL() and COALESCE() Functions
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.
🌐
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.
🌐
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 .
🌐
w3resource
w3resource.com › mysql › control-flow-functions › if-null-function.php
MySQL IFNULL() function - w3resource
-- In this example, the first argument is 0 (which is not NULL), -- and the second argument is 2. SELECT IFNULL(0, 2); -- The expected result will be 0 because the first argument is not NULL.
🌐
Quora
quora.com › Is-null-equal-to-0-in-SQL
Is null equal to 0 in SQL? - Quora
Answer (1 of 3): No, in fact NULL is really never equal to anything, not even itself. So you can’t do something like “select * from BAR where FOO = NULL”. It won’t work since even if FOO is NULL that should not evaluate to true. You need to do something like “Select * from BAR where FOO is null”...
🌐
W3Schools
w3schools.com › sql › func_mysql_ifnull.asp
MySQL IFNULL() Function
SQL Examples SQL Editor SQL Quiz SQL Exercises SQL Server SQL Syllabus SQL Study Plan SQL Bootcamp SQL Certificate SQL Training ... The IFNULL() function returns a specified value if the expression is NULL.
🌐
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