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
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
Discussions

SQL case 0 then NULL - Stack Overflow
SELECT ID, Firstname, Lastname, CASE Number WHEN 0 THEN NULL END FROM tPerson ... At least one of the result expressions in a CASE specification must be an expression other than the NULL constant. ... As others have mentioned you forgot to tell your CASE statement to return the number in case the number is not null. However in SQL Server you can use NULLIF, which I consider more readable: select id, firstname, lastname, nullif(number, 0) as number from tperson; If ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Replace null count with a zero (0)
Look into IFNULL(thing, 0) More on reddit.com
๐ŸŒ r/SQL
22
15
December 22, 2022
Changing NULL Result to Zero in SubQuery ?
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... More on forums.oracle.com
๐ŸŒ forums.oracle.com
April 21, 2023
IF NULL then Zero (0) โ€“ SQLServerCentral Forums
IF NULL then Zero (0) Forum โ€“ Learn more on SQLServerCentral More on sqlservercentral.com
๐ŸŒ sqlservercentral.com
March 30, 2019
๐ŸŒ
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:
๐ŸŒ
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?

๐ŸŒ
Oracle
forums.oracle.com โ€บ ords โ€บ apexds โ€บ post โ€บ changing-null-result-to-zero-in-subquery-6058
Changing NULL Result to Zero in SubQuery ?
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...
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
SQLines
sqlines.com โ€บ teradata โ€บ functions โ€บ nullifzero
Teradata - NULLIFZERO Function - Replace 0 Values with NULL - SQLines Tools
NULLIFZERO function replaces 0 values with NULL, and can be used to avoid division by zero, or to suppress printing zeros in reports i.e. Quick Example: Avoid division by zero: SELECT amount / NULLIFZERO(store_count) FROM sales; NULLIFZERO Overview Summary information:
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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 ...
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ sql โ€บ t-sql โ€บ language-elements โ€บ nullif-transact-sql
NULLIF (Transact-SQL) - SQL Server | Microsoft Learn
The following example creates a ... with budgets that have not changed from the previous year, and 0 is used for budgets that have not yet been determined....
Top answer
1 of 5
2

You do realize you are returning multiple data types in your query? This is not very good query design if the result is a query table.

The data types for 1 and 0 is integer (whether it is int, smallint, or tinyint) while data type for โ€˜โ€™ is character.

You also need to define what โ€˜Blankโ€™ is since this is not a proper value definition in MS SQL. I am going to assume that you want โ€˜Blankโ€™ to be the same as โ€˜โ€™ which is a zero-length string of type character (whether it is char, varchar, nchar, or nvarchar).

Another common problem is the use of โ€˜= NULLโ€™ instead of the proper form โ€˜IS NULLโ€™ since NULL itself is not a value; in simple terms NULL is the absence of a value.

You should read another post here by Larry Shanahan and his included linked post from Robert Sheldon:

Returning a NULL Value when query returns nothing

Post by Robert Sheldon re NULLs

Back to your SQL code - I believe a better simpler version would be one that avoids the use of NULL altogether and would be the following; this also keeps all returned types as character; very close to your 2nd query version:

CASE
    WHEN PAE.SEX = 'F' THEN '1'
    WHEN PAE.SEX = 'M' THEN '0'
    ELSE '' 
    END AS SEX,

@larryshanahan

2 of 5
3

I need to return blank results when there is not a value entered into the table but have only been able to get either a null or 0 value.

CASE

WHEN PAE.SEX = โ€˜Fโ€™ THEN 1
WHEN PAE.SEX = โ€˜Mโ€™ THEN 0
WHEN PAE.SEX = NULL THEN โ€˜โ€™
END AS SEX,

this returns a null value

CASE

WHEN PAE.SEX = โ€˜Fโ€™ THEN 1
WHEN PAE.SEX = โ€˜Mโ€™ THEN 0
ELSE โ€˜โ€™
END AS SEX,

this returns a 0 value

๐ŸŒ
Oracle
forums.oracle.com โ€บ ords โ€บ apexds โ€บ post โ€บ how-to-replace-a-data-0-to-null-1983
how to replace a data 0 to null
May 18, 2009 - In a TABLE, I have a field with data type NUMBER. I am creating a VIEW using the TABLE.field and in that view Im trying to replace the TABLE.field data. That is if data is o it should be replace with ...