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
🌐
W3Schools
w3schools.com › sql › sql_isnull.asp
W3Schools.com
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
🌐
W3Schools
w3schools.com › sql › sql_null_values.asp
SQL NULL Values - IS NULL and IS NOT NULL
The following SQL lists all customers with a value in the "Address" field: SELECT CustomerName, ContactName, Address FROM Customers WHERE Address IS NOT NULL; Try it Yourself » ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: [email protected]
🌐
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.
🌐
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:
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 › mysql › mysql_ifnull.asp
MySQL IFNULL() and COALESCE() Functions
The MySQL IFNULL() function lets you return an alternative value if an expression is NULL. ... 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 ...
🌐
W3Schools
www-db.deis.unibo.it › courses › TW › DOCS › w3schools › sql › sql_isnull.asp.html
SQL ISNULL(), NVL(), IFNULL() and COALESCE() Functions
Below, if "UnitsOnOrder" is NULL it will not harm the calculation, because ISNULL() returns a zero if the value is NULL: ... Oracle does not have an ISNULL() function. However, we can use the NVL() function to achieve the same result: SELECT ProductName,UnitPrice*(UnitsInStock+NVL(UnitsOnOrder,0)) ...
🌐
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.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › how-can-i-return-0-for-null-in-mysql
How can I return 0 for NULL in MySQL?
We can return 0 for NULL in MySQL with the help of IFNULL() method. The syntax of IFNULL() is as follows. IFNULL(YOUREXPRESSION,0); Let us see an example. First, we will create a table.
🌐
W3Schools
w3schools.com › sql › func_sqlserver_nullif.asp
SQL Server NULLIF() Function
SQL Examples SQL Editor SQL Quiz SQL Exercises SQL Server SQL Syllabus SQL Study Plan SQL Bootcamp SQL Certificate SQL Training ... The NULLIF() function returns NULL if two expressions are equal, otherwise it returns the first expression.
🌐
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?

🌐
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.
🌐
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 ...
🌐
W3Schools
www-db.deis.unibo.it › courses › TW › DOCS › w3schools › sql › sql_null_values.asp.html
SQL NULL Values - IS NULL and IS NOT NULL
NULL values represent missing unknown data. By default, a table column can hold NULL values. This chapter will explain the IS NULL and IS NOT NULL operators. If a column in a table is optional, we can insert a new record or update an existing record without adding a value to this column.
🌐
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.
🌐
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.
🌐
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 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.