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.

🌐
TablePlus
tableplus.com › blog › 2019 › 09 › sql-if-null-then-0.html
SQL IF NULL THEN 0 | TablePlus
September 11, 2019 - In MySQL you can also use IFNULL function to return 0 as the alternative for the NULL values: SELECT emp_no, salary, from_date, to_date, IFNULL(bonus, 0) FROM salaries; In MS SQL Server, the equivalent is ISNULL function:
🌐
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] · If you want to report an error, or if you want to make a suggestion, send us an e-mail: [email protected] · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
TutorialsPoint
tutorialspoint.com › how-can-i-return-0-for-null-in-mysql
How can I return 0 for NULL in MySQL?
mysql> select IFNULL(id, 0) from NullDemoWithZero; The following is the output that returns 0 for every NULL.
🌐
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?

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.
🌐
Apache OpenOffice Community
forum.openoffice.org › board index › applications › base › tables & queries
Apache OpenOffice Community Forum - [Solved] Query returning zero instead of null - (View topic)
The WHERE clause demonstrates that the table cell is still NULL from the SQL viewpoint. So, it appears the developers of AOO must have made the philosophical decision that once NULL has been cast to a numeric value they will display 0, the mathematical representation. If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
🌐
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 - Otherwise, it returns the value of the expression. Let’s rewrite the earlier query using the IFNULL function: ... 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, ...
🌐
W3Schools
w3schools.com › mysql › func_mysql_ifnull.asp
MySQL IFNULL() Function
If the expression is NOT NULL, this function returns the expression. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: [email protected] · If you want to report an error, or if you want to make a suggestion, send us an e-mail: [email protected] · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL ...
🌐
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 .
🌐
Modern SQL
modern-sql.com › concept › null
Modern SQL: NULL — purpose, comparisons, NULL in expressions, mapping to/from NULL
The example returns the result of the expression, unless it is null, then it returns zero (0). Coalesce takes an arbitrary number of arguments and returns the first not null value or null if all arguments are 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