You can use a CASE statement.

SELECT 
    CASE WHEN currate.currentrate IS NULL THEN 1 ELSE currate.currentrate END
FROM ...
Answer from Justin Helgerson on Stack Overflow
🌐
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
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:
🌐
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.
Find elsewhere
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

Top answer
1 of 4
8

I think you are making it harder than it should be.

If @UserRole is 'Analyst' then also SupervisorApprovedBy should be null? Else return everything?

WHERE (@UserRole = 'Analyst' AND SupervisorApprovedBy IS NULL ) 
OR (ISNULL(@UserRole, '') <> 'Analyst')
2 of 4
13

The problem is likely the comparison to NULL, as explained in David Spillett's answer above. When @UserRole = 'Analyst', the comparison SupervisorApprovedBy = NULL will give UNKNOWN (and the row won't pass the WHERE test).

You can rewrite with nested CASE expressions:

WHERE 1 =
  CASE 
    WHEN @UserRole = 'Analyst' THEN 
        CASE WHEN SupervisorApprovedBy IS NULL THEN 1 END
    WHEN SupervisorApprovedBy IS NOT NULL THEN 1
  END

Or a complicated CASE expression:

WHERE 1 =
  CASE 
    WHEN @UserRole = 'Analyst' AND SupervisorApprovedBy IS NULL THEN 1
    WHEN @UserRole = 'Analyst' THEN 0
    WHEN SupervisorApprovedBy IS NOT NULL THEN 1
  END

or with a bit more easy to understand AND / OR:

WHERE 
    @UserRole = 'Analyst'  AND SupervisorApprovedBy IS NULL 
 OR @UserRole <> 'Analyst' AND SupervisorApprovedBy IS NOT NULL 
 OR @UserRole IS NULL      AND SupervisorApprovedBy IS NOT NULL 

Another issue is that SupervisorApprovedBy = SupervisorApprovedBy (and the equivalent SupervisorApprovedBy IS NOT NULL I used above) will not give you "all data". The rows where SupervisorApprovedBy is null will not be returned. If you do want them all, the conditions should be all adjusted:

WHERE 1 =
  CASE 
    WHEN @UserRole = 'Analyst' THEN 
        CASE WHEN SupervisorApprovedBy IS NULL THEN 1 END
    ELSE 1
  END


WHERE 1 =
  CASE 
    WHEN @UserRole = 'Analyst' AND SupervisorApprovedBy IS NULL THEN 1
    WHEN @UserRole = 'Analyst' THEN 0
    ELSE 1
  END


WHERE 
    @UserRole = 'Analyst'  AND SupervisorApprovedBy IS NULL 
 OR @UserRole <> 'Analyst' 
 OR @UserRole IS NULL       
🌐
w3resource
w3resource.com › mysql › control-flow-functions › if-null-function.php
MySQL IFNULL() function - w3resource
Hence, the query returns 0 as the output. This function is useful for providing default values when handling potential NULLs in SQL queries. ... mysql> SELECT IFNULL(0,2); +-------------+ | IFNULL(0,2) | +-------------+ | 0 | +-------------+ 1 row in set (0.03 sec)
🌐
Modern SQL
modern-sql.com › feature › case
SQL CASE works in practically all SQL-Databases
Even if the demo table is empty, the error can still happen, if the constant expression 1/0 is evaluated during the prepare phase. SELECT CASE WHEN id = 0 THEN 1/0 ELSE 1 END FROM demo
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 911838 › sql-query-needs-to-handle-both-null-and-integer-va
Sql query needs to handle both NULL and INTEGER value. - Microsoft Q&A
CREATE OR ALTER PROCEDURE Vamshi_sp @list nvarchar(MAX) AS ; WITH CTE AS ( SELECT COUNT (DISTINCT WN.APPKEY) AS appkeycnt FROM dbo.VW_NUMBER WN WHERE WN.ANUMBER IN (SELECT value FROM string_split(@list, ',')) AND WN.UNITKEY IN (SELECT IC.UNITKEY FROM dbo.VW_DATA IC WHERE IC.UNITNAME = 'MAS') GROUP BY WN.DEPTKEY, WN.ANUMBER ), CTE2 AS ( SELECT MIN(appkeycnt) AS mincnt FROM CTE ) SELECT CASE WHEN mincnt IS NULL THEN NULL WHEN mincnt = 1 THEN 1 ELSE 0 END, mincnt FROM CTE2 go EXEC Vamshi_sp 'GNW0060077,0456618' EXEC Vamshi_sp 'NGL0300008,235493' EXEC Vamshi_sp 'UY1233,VAMPQ34' EXEC Vamshi_sp 'GNW0060077,0456618,NGL0300008,235493,UY1233,VAMPQ34' EXEC Vamshi_sp 'GNW0060077,UY1233,NGL0300008'
🌐
Snowflake Documentation
docs.snowflake.com › en › sql-reference › functions › ifnull
IFNULL | Snowflake Documentation
INSERT INTO suppliers(supplier_id, supplier_name, phone_region_1, phone_region_2) VALUES(1, 'Company_ABC', NULL, '555-01111'), (2, 'Company_DEF', '555-01222', NULL), (3, 'Company_HIJ', '555-01333', '555-01444'), (4, 'Company_KLM', NULL, NULL); ... The following SELECT statement uses the IFNULL function to retrieve the phone_region_1 and phone_region_2 values.
🌐
StrataScratch
stratascratch.com › blog › introduction-to-ifnull-function-in-sql
Introduction to IFNULL() Function in SQL - StrataScratch
WITH a AS (SELECT user_firstname, user_lastname, MAX(reviewed_date) AS latest_date FROM user_flags u JOIN flag_review r USING (flag_id) WHERE reviewed_by_yt = 1 GROUP BY user_firstname, user_lastname), b AS (SELECT reviewed_date, IFNULL(COUNT(DISTINCT video_id), 0) AS num_video FROM flag_review r JOIN user_flags f USING (flag_id) WHERE reviewed_outcome = 'REMOVED' GROUP BY reviewed_date) SELECT a.user_firstname, a.user_lastname, a.latest_date, IFNULL(b.num_video, 0) AS num_video FROM a LEFT JOIN b ON a.latest_date = b.reviewed_date · So, we have included the user’s first and last names from
🌐
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 - Here are some of the other methods that can replace null with 0 in SQL. Syntax: COALESCE(column_name, 0) Query: SELECT Sales_Id, COALESCE(amount, 0) AS amount FROM sales; Syntax: CASE WHEN column_name IS NULL THEN 0 ELSE column_name END · Example: SELECT Sales_Id, CASE WHEN amount IS NULL THEN 0 ELSE amount END AS amount FROM sales; If you want to learn about SQL, you may refer to interview questions: SQL Interview Questions and Answers.