You can use NVL:

NVL(col, 0)

or COALESCE:

COALESCE(col, 0)
Answer from Gurwinder Singh on Stack Overflow
🌐
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: SELECT emp_no, salary, from_date, to_date, ISNULL(bonus, 0) FROM salaries; ...
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?

🌐
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
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
🌐
Oracle-Base
oracle-base.com › articles › misc › null-related-functions
NULL-Related Functions - ORACLE-BASE
If we query the table we see the following data. SELECT * FROM nanvl_test_tab ORDER BY id; ID COL1 ---------- ---------- 1 1.235E+003 2 Inf 3 -Inf 4 Nan 4 rows selected. SQL> Next, we query the data again, but convert any "NaN" values to "0" using the NANVL function.
Find elsewhere
🌐
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 ...
🌐
Oracle Analytics
community.oracle.com › oracle analytics › oracle analytics forums › more solutions › oracle transactional business intelligence
Showing '0' for Null Value — Oracle Analytics
March 3, 2018 - ... Could it be a datatype issue? ifnull() works for me with both string and numeric data types without any additional conversion. ... You can try "Edit Formula" for that column and use "IF-THEN" condition to replace NULL with 0.
🌐
SQLServerCentral
sqlservercentral.com › forums › topic › nullif-and-0
NULLIF and 0 – SQLServerCentral Forums
March 25, 2021 - 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.
🌐
Oracle
forums.oracle.com › ords › apexds › post › changing-null-result-to-zero-in-subquery-6058
Changing NULL Result to Zero in SubQuery ? - Oracle Forums
April 21, 2023 - Hi Everyone, I'm working on a project an having difficulty getting the correct results. I'm using a sub query to get a value, but the values come back NULL and I need to display NULL as ze...
🌐
TechOnTheNet
techonthenet.com › oracle › functions › nvl.php
Oracle / PLSQL: NVL Function
This SQL statement would return the supplier_name field if the supplier_desc contained a null value. Otherwise, it would return the supplier_desc. A final example using the NVL function in Oracle/PLSQL is: ... This SQL statement would return 0 if the commission field contained a null value.
🌐
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]
🌐
Oracle Tutorial
oracletutorial.com › home › oracle basics › oracle is null operator
Oracle IS NULL Operator
April 27, 2025 - The expression returns true if the value is not blank or false otherwise. We’ll use the orders table from the sample database. In the orders table, the salesman_id column stores the id of the salesman who is in charge of the sales order. The following SELECT statement attempts to retrieve all sales orders that do not have a dedicated salesman: SELECT * FROM orders WHERE salesman_id = NULL ORDER BY order_date DESC;Code language: SQL (Structured Query Language) (sql)
🌐
TechOnTheNet
techonthenet.com › oracle › isnull.php
Oracle / PLSQL: IS NULL Condition
You can use the Oracle IS NULL condition in either a SQL statement or in a block of PLSQL code. The syntax for the IS NULL condition in Oracle/PLSQL is: ... The value to test whether it is a null value. If expression is a NULL value, the condition evaluates to TRUE.