SQL ISNULL - Check If A Given Value Is Null Or Not

SQL Isnull and Case

The CASE statement will return the value in the ELSE clause if no value/condition is discovered to be TRUE. The CASE statement will return NULL if the ELSE clause is missing and no condition is discovered to be true.

CASE x WHEN null THEN and CASE x = null THEN are the same thing. In that sense, though, nothing equals null. This means that the ELSE component of your CASE statement is always executed. That suggests you're attempting to concatenate a string with NULL, which will always result in NULL.

SQL Isnull and Cast

CAST(field name AS CHAR) converts the field name column to a CHAR.

The first input to ISNULL() is then this value, with the second being an empty string ".

ISNULL(CAST(field_name AS CHAR), '')

If the cast result is a NULL value, this function returns " instead.

Finally, it compares the result to the value ".

In essence, it checks for NULL or empty string values all at once.

SQL Isnull and Count

Example 1: Return null if certain dates are selected, I want to display 0 instead of a blank but cannot figure it out:

* I removed the date code to make it easier:

SELECT ISNULL(COUNT(*), '0') AS thecount
FROM dbo.table1
WHERE (dbo.table1.school = 'xxxxxxxxxxxxx')
GROUP BY dbo.table1.school

Example 2: You'll notice that the query states there are no NULL values, despite the fact that we have three NULL values. This is due to the fact that all aggregate values ignore NULL values. If you wish to count the NULL values, you'll need to convert them to different values first, then use the aggregate function, as seen in the following script.

FROM Table1

When you run the above script, you'll note that the query now correctly displays NULL values. The reason for disregarding the NULL value is simple: it prevents unknown or inapplicable values from influencing the aggregate's result.

Example 3: If you want to count the nulls, here's a quick way to do that. First, tell me what field you're trying to count, and then tell me which fields in that row aren't null.

So we'll call this table person because it contains four columns: id, FirstName, LastName, and Email. If we know that FirstName cannot be null and that email can be null, and we want to know how many email fields are null, we can write our query as follows:

FROM Person

I tried the same query with Select Count Email and I received 0 records but when I switched it to a non nullable field then I got back the actual count.

SQL Isnull and Ifnull

The ISNULL() method in MySQL is used to check for NULL values in an expression that is supplied as a parameter. It shows 1 if the expression has/results NULL. The function returns 0 if the expression does not contain or result in NULL.

If the expression is NULL, the MySQL IFNULL() function returns a defined value. IFNULL() returns the expression if the expression is not NULL.


SELECT ID, FirstName, IF(ISNULL(Work)=1, 'N/A', Work) AS 'Work Number' FROM Persons;

Monitoring for NULL values and handling them while presenting results is an important database activity, and MySQL's ISNULL() and IFNULL() functions are essential for this.

SQL Isnull and Isnotnull

NULL data denotes data that does not exist, is missing, or is unknown. IS NULL and IS NOT NULL are SQL operators that are used with a WHERE clause in SELECT, UPDATE, and DELETE statements/queries to determine whether a column has data or not. Please keep in mind that NULL and 0 are not the same thing. A column with a NULL value is empty and has no value.

When executing Boolean operations on values that include NULL, NULL can also be used as a keyword. For this, the "IS/NOT" keyword is used in conjunction with the NULL word.

The Boolean comparison is performed using the keyword "IS NULL." If the specified value is NULL, it returns true; if the supplied value is not NULL, it returns false.

The Boolean comparison is performed via the keyword "NOT NULL." If the given value is not NULL, it returns true; otherwise, it returns false.

If the column has a null value or if the expression cannot be analyzed because it holds one or more null values, the IS NULL condition is fulfilled. The condition is met when the operand is a column value that is not null or an expression that does not evaluate to null when using the IS NOT NULL operator.

Syntax for SQL IS NULL

SELECT column_name1, column_name2, etc
FROM table_name
WHERE  column_name1 IS NULL;

Syntax for SQL IS NOT NULL

SELECT column_name1, column_name2, etc
FROM table_name
WHERE  column_name1 IS NOT NULL;


IS NULL and IS NOT NULL can be used in the WHERE clause of the same SQL query in any order and in any combination as needed.

IS NULL and IS NOT NULL can also be used to set the property of each column when building new tables. That is, whether the column can take a NULL value or whether it must have a value.

Example 1: Let's look at a real-world example where the NOT NULL keyword is used to delete all column values with null values.

Continuing with the previous example, let's say we're looking for information on members who have a phone number that isn't zero. We can run a query like this.

SELECT * FROM `members` WHERE contact_number IS NOT NULL;

Executing the above query gives only records where contact number is not null.

Example 2: If the phrase immediately preceding the IS keyword specifies one of the following undefined values, the IS NULL condition is satisfied:

  • An expression that evaluates to null.
  • The name of a column that has no data in it.

When you employ the IS NOT NULL operator, however, the condition is satisfied if the column has a value that is not null or if the expression that comes before the IS NOT NULL keywords does not evaluate to null.

Assume you want to do an arithmetic calculation on a column that can have NULL values in it. You can construct a table, populate it with values, and then run a query that uses a generic CASE expression to convert null values to 0 for arithmetic calculations:

CREATE TABLE employee (emp_id INT, savings_in_401k INT, total_salary INT);  

INSERT INTO employee VALUES(1, 5000, 40000); 
INSERT INTO employee VALUES(2, 0, 40000); 
INSERT INTO employee VALUES(3, NULL, 100000);  

SELECT emp_id, savings_in_401k AS employer_match FROM employee WHERE
CASE WHEN(savings_in_401k IS NULL) THEN 0
ELSE savings_in_401k END * 0.06 > 0;  

This example demonstrates how you may supply a value for entries that would otherwise be uncomputable since null is not a valid numeric value by utilising IS NULL in the CASE statement.

Example 3: The field outcome type in our dataset's outcome table contains the varied outcomes for the animals at the centre. Subtypes stored in the outcome_ subtype column help to specify some of the possible outcomes. If we just wanted data with both an outcome type and an outcome_subtype, we could write the query like this:

SELECT monthyear,
FROM austin_animal_center_outcomes
WHERE outcome_subtype IS NOT NULL;

SQL Isnull and Nullif

Main Article :- Sql difference between ISNULL() and NULLIF() Functions

The terms ISNULL and NULLIF are diametrically opposed. If the comparison is successful, ISNULL returns NULL.

If the two supplied expressions are equivalent, NULLIF returns null.

ISNULL Syntax:

ISNULL ( the Check _ expression The , replacement_value )


  • check_expression: It will be checked for the NULL expression. check_expression can be of any type.
  • replacement_value: In check_expression is NULL when the return expression. replacement_value must check_expresssion have the same type .
  • Return Type: Returns check_expression same type .

Note: If check_expression is not NULL , then the return value of the expression; otherwise it returns replacement_value.

NULLIF Syntax:

NULLIF ( expression The , expression The )


expression: Constant, column name, function, subquery or arithmetic operators, bitwise operators in any combination, and string operators.

Return Type:

  • The first expression's return type is the same.
  • NULLIF returns the first expression value if the two expressions are not equal. NULLIF returns the first expression type null if they are equal.

Note: NULLIF is the corresponding CASE search function if the two expressions are equal and the resultant expression is NULL.

Example 1: If any value in the LName column is Null, replace it with "Unknown." In our Select statement, we also want to add a new output column called "ColValueNotEqual." We want to retrieve the FName value if FName and LName are different, and we want to get Null for this column if FName and LName are the same.

Let's use ISNULL ( ) and NULLIF ( ) functions to get required results.

       ISNULL([LName], 'Unknown') AS LName,
       NULLIF(FName, LName)       AS ColValueNotEqual
FROM   #tmp 

Because we used the ISNULL() function, the null value of LName is substituted by "Unknown." Because we utilised the NULLIF() function, ColValueNotEqual returns FName when FName and LName are not equal and Null when FName and LName column values are equal.

SQL Isnull and Replace

SQL Server's ISNULL Function ISNULL is a built-in function that can be used to replace nulls with specified replacement values. To use this function, simply supply the column name as the first parameter and the value you wish to replace the null value with as the second parameter.

Syntax of the ISNULL function.

ISNULL(value, replacement-value)
  • value -- a value or column name (which is checked for NULL).
  • replacement-value -- a value to replace the NULL value with.

Example 1: First, the ISNULL function will be "run," which means that if COL01 is NULL, an empty string will be returned and sent to the REPLACE function, which will replace all '.' with a ','. If the value was NULL, the function will return an empty string.


Example 2: When you want to replace a possibly null column with something else, use IsNull.

SELECT ISNULL(myColumn, 0 ) FROM myTable 

If myColumn is null in the first place, this will set it to 0.

It's worth noting that the return type of the ISNULL function is the same as the first parameter's data type.

Example 3:

Select id,Name,Phone, ISNULL(salary,0) as [Salary] from Employee

Null values can be replaced in SQL by searching a column in a table for nulls and replacing them with UPDATE, SET, and WHERE. It substitutes them with 0 in the case above. Because untidy data can lead to inaccurate analysis, cleaning data is critical for analytics. Null values are a typical type of incorrect data.

SQL Isnull and Sum

Example 1: Find the sum of salary of all Employee, if Salary of any employee is not available (or NULL value), use salary as 10000.

SELECT SUM(ISNULL(Salary, 10000) AS Salary
FROM Employee;

Example 2: SQL ISNULL can also be used with aggregate methods like AVG and SUM. Assume we wish to execute the SUM of StudentFees in the Student database. Before adding the Fees, if StudenFees is NULL, it should be substituted with 20000. additionally, before we go, do the following query to set StudentFees to NULL for StudentID 2.

Update Student set StudentFees =NULL where StudentID =2;

Example 3: First, I replaced the NULL value with the value 20000, and then I did an SUM on it. Additionally, you can see the result in the screenshot below.

SELECT SUM(ISNULL(StudentFees, 20000))
FROM Student;

SQL Isnull Convert Date

Example 1: Setting a default NULL Date in SQL is simple; all you need to do is focus.

Any NULL value inside the relevant DATE column will be converted to '01/01/1900' with the following statement.


Example 2:

declare @DateSent date = getdate()
Select ISNULL(Cast(@DateSent as Varchar(20)), 'N/A')

For null values as below.

declare @DateSent1 date = NULL
Select ISNULL(Cast(@DateSent1 as Varchar(20)), 'N/A')

SQL Isnull and Divide by Zero

The outcome is 0 if the denominator or divisor is 0. Aside from that, the division operation is carried out. In some instances, returning a value other than NULL may be preferable.

Use nullif on the denominator to make the denominator null instead of zero, which is one of null's advantages.

Example 1: Using the NULLIF and ISNULL functions, your query will look like the following:

SELECT ISNULL([Numerator] / NULLIF([Denominator], 0), 0) AS [Percentage]
FROM [Table1]

If the denominator is zero, this will transform it to NULL. Then, when any number is divided by NULL, the result is NULL. As a result, if the denominator is 0, the division result will be NULL. The ISNULL function is then used to return a value of 0 instead of a NULL value.

Example 2: In such cases, you can pass the previous example to the ISNULL() function:

SELECT ISNULL(1 / NULLIF( 0, 0 ), 0);



When the result is NULL, I specified that zero should be returned.

But be cautious. In some circumstances, returning zero isn't the best option. If you're working with inventory supplies, for example, specifying zero could imply that there are no products, which isn't always the case.

Example 3: Errors involving division by zero are particularly excruciating. Queries that worked great the day before are now failing due to divide by zero issues. One frequent technique is to use a case statement to verify if the denominator is 0 before dividing:

select case when num_users = 0 then 0 
else total_sales/num_users end;

The case statement approach is verbose and duplicates the denominator. That’s OK if the denominator is simple, but if it’s an expression, you’re likely to get more bugs if you change the query later.

Example 4: Rather than failing, the query will return null on days where num_users = 0:

select total_sales/nullif(num_users, 0);

Example 5: If you prefer the result to be 0 or anything else instead of null, use coalesce on the previous query:

select coalesce(total_sales/nullif(num_users, 0), 0);



SQL Isnull for Datetime

I have a date and date column with a null value.

Running DATE (col) and CAST (col AS DATE) returns null for rows with null values.

ISNULL (DATE (col)) and ISNULL (CAST (col AS date)) are always 0. This means that rows with null values ​​are no longer null when casting.

This is a big problem with nested views if you format the first view and then use the first view to create a WHERE clause in the second view.

Example 1:

mysql> SELECT ISNULL(DATE(null));



Example 2:

mysql> SELECT ISNULL(TIME(null));



SQL Isnull for Multiple Columns

Only one value can be checked with the IsNull function. For numerous values, it is unable to check null. That is, it is unable to perform the feature of verifying if the first argument is null before moving on to the following parameter's null check.

Syntax to find rows with NULL values in many columns in MS SQL Server.

FROM [table_name] 
WHERE [column1] IS NULL AND [column2] IS NULL AND [columnN] IS NULL;

Example 1: Null values are frequently seen in SQL Server tables. Null values are values that have no data, implying that the data is either absent or unknown. In this tutorial, we'll learn how to use the SQL Server IsNull function before moving on to using the Coalesce function to enhance the IsNull capabilities.

Let us see the following example.

create table Identification
(empid Integer,
ename varchar(30) NOT NULL,
passport_number char(15) ,
license_number char(15) ,
pan char(15) ,
credit_card_number char(15) ,
account_number char(15) 

insert into identification values(1,'John',null,null,'PN-78654','CC-12345','AN-3456')
insert into identification values(2,'Martin','PS-566774',null,null,null,null)
insert into identification values(3,'Smith',null,null,null,null,null)
insert into identification values(4,'Roger',null,null,null,null,'AN-9876')
insert into identification values(5,'King',null,null,null,'CC-8787','AN-9878')
select * from identification

In the above table, every employee has an identity proof which is either a passport number, license number, pan, credit card or account number.

Example 2: Suppose we want to see if an employee has a passport or not, here the IsNull function can help us.

See the following example of using SQL Server ISNULL in a Select Statement:

select empid, ename, IsNull(Passport_Number, 'Not Found') 
as 'Passport Status' from identification

SQL Isnull

ISNULL is a SQL Server built-in function. It allows the user to specify a replacement value for NULL values. The ISNULL function returns an alternate value if the expression or table records have NULL values, as explained in this article.

SQL ISNULL Function Syntax

SELECT ISNULL ([Check_Expression], 
FROM [Source]

If you look at the code above, you'll notice that the SQL ISNULL function accepts two arguments:

  • Check Expression: Please enter the valid expression or column name to check for NULL values. The NULL values in this column will be checked by this function.
  • Replacing_Expression: Please provide a valid expression to replace the NULL with. This function will use this Replacing_Expression to replace the NULL values in the Check_Expression.

TIP: Replacing_Expression will be returned if Check_Expression is NULL. Check_ Expression will return if this is not the case.

This function substitutes the NULL value with the replacement value if the expression evaluates to NULL. When the data types of the arguments differ, the SQL server converts the replacement value data type to the expression data type implicitly before returning a value. When the expression is not NULL, we will retrieve the value of the expression.

The ISNULL function is used in the example below. Because the first parameter is NULL in this case, the value of the second argument is returned as a result.

Example 1: The below example uses the ISNULL function. Here the first argument is NULL; therefore, it returns the value of a second argument in a result:


Example 2: Using SQL Server ISNULL() function with the numeric data example:

This example uses the ISNULL() function to return the second argument because the first argument is NULL:




SQL Isnull or Empty

We can use the where clause with IS NULL to check if a column is empty or null, and we can use the condition" to verify if a column is empty.

In SQL, for example, when concatenating Strings, replace NULL values with empty Strings or blanks. When you concatenate a NULL String with another non-null String in SQL Server, the result is NULL, which means the information you already have is lost. You can avoid this by replacing NULL with an empty String while concatenating.

If a variable has no value and corresponds to nowhere in memory, it is called NULL. empty() has a more literal meaning than NULL; for example, the string " " is empty but not NULL.

isEmpty looks for strings with no characters or whitespace; isBlank returns a boolean value that indicates whether the string is null or not. Checks if the value of is an empty string with no characters or whitespace. utilises the isNull operator to see if the country_code field value is null.

I've had a result set that generates rowsets with null column entries in it. 2) (*if null, make empty string*) how_high There are two tables, one of which includes a few null fields. The following sql returns nulls as a result. I've discovered that the NVL will only replace (null) with a numeric value, as in.


To check if the column has null value or empty, the syntax is as follows:

SELECT * FROM yourTableName WHERE yourSpecificColumnName IS NULL OR
yourSpecificColumnName = ' ';

The IS NULL constraint can be used whenever the column is empty and the symbol ( ‘ ‘) is used when there is empty value.

Now, the query using the above syntax is given as follows:

SELECT * FROM ColumnValueNullDemo WHERE ColumnName IS NULL OR
ColumnName = ' ';

Example 1: Used on occasion to change all of the null values to empty strings and then compare the result to an empty string.

select * from vendor
where isnull(vendor_email,'') = ''

Example 2: You could perform something like this to search for the SQL null or empty string together and obtain all of the empty strings and nulls at once.

select * from vendor
where vendor_email = ''
or vendor_email is null;

Example 3:

SELECT * FROM ColumnValueNullDemo WHERE ColumnName IS NULL OR
ColumnName = ' ';



This output was obtained as the second condition is true for empty value.

SQL Isnull or Whitespace

ISNULLORWHITESPACE examines the value in expression to see if it is Null, empty, or contains nothing but one or more spaces (whitespace), and returns True or False depending on the result. Space, tabs, and certain Unicode characters labelled as blanks are all examples of whitespace.



Arguments: expression Any valid expression, a Null value, empty, or whitespace.

Returns: If expression is Null, empty, or a string containing only whitespace, returns a Boolean True.

Example 1: The following formula checks to see if the value for MyEntity.LastName is Null, empty, or contains nothing but spaces, and if so, provides a default value:

IF(ISNULLORWHITESPACE(MyEntity.LastName), "MissingLastName", MyEntity.LastName)

Example 2: IF #value = '' and if #value = NULL and neither one catches the blank values. Is there any way to test whether or not a varchar is entirely whitespace?

AHA! Turns out I was testing for null wrong. Thanks.

To compare with NULL, use the IS NULL keyword.

--Generic example:

--Instead of

SQL Isnull Return 0

The ISNULL() function in MySQL is used to determine whether or not an expression is NULL. If the expression supplied is NULL, this method returns 1, otherwise it returns 0.



Parameters Used:

expression – It is used to specify the expression.

Return Value: If the expression supplied is a NULL expression, the MySQL ISNULL() method returns 1, otherwise it returns 0.

Example 1: Implementing ISNULL() function on an integer value.




Example 2:

select isnull ('replace value of the NULL') AS "IS NULL";



Here in the above expression value is not NULL because of which the value that is returned is 0.

SQL Isnull Return Boolean

Returns a Boolean value indicating whether or not an expression is valid (Null).


IsNull ( expression )

A Variant holding a numeric or string expression is needed as an expressionargument.

If expression is Null, IsNull returns True; otherwise, IsNull returns False. Null in any constituent variable causes True to be given for the entire expression if the expression has more than one variable.

SQL Isnull Return Value

The ISNULL() function can be used to verify and replace a NULL-returning expression or literal value with a different value.


The above statement will fail in MySQL, but MSSQL will perform the function and return the string value 'Hello'.

SQL Isnull Then 0 Else 1

Example: The exchange rate value set in our system is returned. Because an exchange rate (currate.currentrate) is not available for every order, it returns null values.

    CASE WHEN currate.currentrate IS NULL THEN 1 ELSE currate.currentrate END
FROM ...

SQL Isnull vs Coalesce

The COALESCE expression and the ISNULL function both have the same goal, although they can respond differently. COALESCE is more flexible and powerful than ISNULL, which is the most significant distinction between both.

ISNULL is analyzed only once because it is a function. The COALESCE expression's input values can be evaluated several times.

COALESCE respects the CASE expression rules and returns the data type of the value with the highest precedence, whereas ISNULL uses the data type of the first parameter.

The ISNULL return value is always considered NOT NULLable (assuming the return value is a non-nullable one). By contrast,COALESCE with non-null parameters is considered to be NULL. So the expressions ISNULL(NULL, 1) and COALESCE(NULL, 1), although equal, have different nullability values.

With ISNULL(), you can only provide one alternate value but with COALESCE you can provide more than one e.g. if col1 IS NULL then take value from column2, if that is NULL then take the default value.

Example 1: These values make a difference if you're 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:

USE tempdb;  
-- This statement fails because the PRIMARY KEY cannot accept NULL values  
-- and the nullability of the COALESCE expression for col2   
-- evaluates to NULL.  
  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.  

  col1 INTEGER NULL,   
  col2 AS COALESCE(col1, 0),   
  col3 AS ISNULL(col1, 0) PRIMARY KEY   

Validations for ISNULL and COALESCE are also different. For example, a NULL value for ISNULL is converted to int though for COALESCE, you must provide a data type.

ISNULL takes only two parameters. By contrast COALESCE takes a variable number of parameters.

Example 2: The ISNULL() function contains only two parameters. The COALESCE() function contains multiple parameters. If we use more than two parameters with the ISNULL function then we must use nested ISNULL functions.

declare @test varchar(3)  
select isnull(@test, 'ABCD') AS ISNULLResult  
select coalesce(@test, 'ABCD') AS coalesceResult;

The ISNULL() function contains various types of parameters. The COALESCE() function doesn't limit the number of arguments, but they must all be of the same data type.

Example 3: You can provide COALESCE multiple values to use in case the target is NULL. For example, in the following query, we have provided four options to COALESCE:


--This will return SQL

SELECT COALESCE(@x,@y,@z,@a)

With ISNULL, you can only provide two values e.g.


Example 4: It should be remembered that the expression returned by function ISNULL, is considered by the SQL server as not NULL, and COALESCE on the contrary allowing NULL. This can be seen, for example, when creating a primary key constraint on the calculated column, i.e. using ISNULL(NULL, 1) – this can be done, but not with COALESCE(NULL, 1):


SET @Var1 = NULL
SET @Var2 = NULL
SET @Var3 = 1

ISNULL(@Var1, @Var2) AS [ISNULL] --Error if you specify the third parameter

SQL Isnull Where Clause

IS NULL in where clause. We use it to replace NULL values with a Particular value.

NULL is a unique value that signifies unknown or no value.

Testing for NULL with the = operator is not possible.

Example 1: List customers that have not placed any orders.

SELECT C.Id, FirstName, LastName, TotalAmount
  FROM Customer C
  LEFT JOIN [Order] O ON C.Id = O.CustomerId
 WHERE TotalAmount IS NULL

Example 2: identify records with the NULL value: the solution is to use the SQL IS NULL operator. Here is an example query:

SELECT year, apple_variety, tons_produced
FROM   apples
WHERE  first_summer_storm IS NULL


year apple_variety tons_produced
2015 Red Delicious 68
2014 Red Delicious 71

Example 3: Use comparison operators such as =, <, > etc in where clause for conditions. However when a column (field) of table has null values then such operators do not work on those columns, in such case we have to use IS NULL & IS NOT NULL operators for the null check.

SELECT column_name1, column_name2, column_name3, ...
FROM table_name
WHERE column_nameN IS NULL;