Isnull VS Is Null

isnull vs is null

where isnull(name,'') <> ''

is equivalent to

where name is not null and name <> '' 

which in turn is equivalent to

where name <> ''

(if name IS NULL that final expression would evaluate to unknown and the row not returned)

The use of the ISNULL pattern will result in a scan and is less efficient as can be seen in the below test.

SELECT ca.[name],
[number],
[type],
[low],
[high],
[status]
INTO TestTable
FROM [master].[dbo].[spt_values]
CROSS APPLY (SELECT [name]
UNION ALL
SELECT ''
UNION ALL
SELECT NULL) ca

CREATE NONCLUSTERED INDEX IX_TestTable ON dbo.TestTable(name)

GO

SELECT name FROM TestTable WHERE isnull(name,'') <> ''

SELECT name FROM TestTable WHERE name is not null and name <> ''
/*Can be simplified to just WHERE name <> '' */

Which should give you the execution plan you need.

Sample Image

Differences between IS NULL and ISNULL() in Mysql

Looking into the MySQL manual, they seem to be synonyms really.

  • MySQL manual on IS NULL

  • MySQL manual on ISNULL()

and even if they aren't, I would tend to trust the query optimizer to pick the best solution.

SQL Server - NULL vs blank in IF condition - ISNULL vs COALESCE

Since your variable is null, you can't concat it with the string in your if or else clause.
You must remove it...

DECLARE @V_MY_VAR VARCHAR(50) = NULL;
IF ISNULL(@V_MY_VAR,'X') = 'HELLO'
BEGIN
PRINT 'INSIDE IF - ';
END;
ELSE
BEGIN
PRINT 'INSIDE ELSE - ';
END;

...or replace it by a non null value, as example using your propose ISNULL.

DECLARE @V_MY_VAR VARCHAR(50) = NULL;
IF ISNULL(@V_MY_VAR,'X') = 'HELLO'
BEGIN
PRINT 'INSIDE IF - '+ ISNULL(@V_MY_VAR,1);
END;
ELSE
BEGIN
PRINT 'INSIDE ELSE - '+ ISNULL(@V_MY_VAR,2);
END;

You can test this here: db<>fiddle

java.util.Objects.isNull vs object == null

should use object == null over Objects.isNull() in a if statement?

If you look at the source code of IsNull method,

 /* Returns true if the provided reference is null otherwise returns false.*/

public static boolean isNull(Object obj) {
return obj == null;
}

It is the same. There is no difference. So you can use it safely.

ISNULL() OR Is NULL in UPDATE statement

Those queries won't do the same thing.

Update Table set REC_ID = isnull(REC_ID,'')

This one will update each record and if REC_ID is NULL it will set it to ''.

Update Table set REC_ID = '' where REC_ID is NULL

This one will only update records containing a null value in REC_ID, and set it to ''.

While both of them will end up giving the same result, the second one will be executed on less records (except if every REC_ID is NULL), it should be faster.

SQL - Difference between COALESCE and ISNULL?

Comparing COALESCE and ISNULL

The ISNULL function and the COALESCE expression have a similar purpose but can behave differently.

  1. 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.
  2. 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.
  3. 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.
> USE tempdb; 
> GO

> -- 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 );

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.
ISNULL takes only 2 parameters whereas COALESCE takes a variable
number of parameters.

Source: BOL

Which one is better: NOT EXIST [data] vs ISNULL([data], '') = 0

In an attempt to find a row where a specific column has a null value, one of my colleague verify if a data in a column is null by making the data empty if null:

...WHERE ISNULL([column], '') = ''

Regardless of any other efficiencies that can be found, it seems a bit silly to use ISNULL() when instead this condition can very easily be expresseed as:

WHERE [column] IS NULL

Now, in cases when you are needing to test to see if "at least 1" row meets a particular condition, then yes, using EXISTS is nearly always (maybe even always) more efficient than not using EXISTS because the EXISTS operator is designed to exit upon finding the first row. When no rows are returned then maybe both methods are equivalent, but if at least one row is present, then the EXISTS is definitely better because it won't process any additional result rows since the answer has already been logically determined as true. The efficiency is more apparent when there are many rows that meet the filtering condition(s).

Hence, the most efficient means of doing this particular test would be:

IF (EXISTS(SELECT * FROM [Table] WHERE [Column] IS NULL))
BEGIN
...
END;

So yes, try to always use EXISTS (whenever possible). And please read the MSDN page for the EXISTS operator as it has additional info regarding using it in WHERE clauses.

For info on how to test for efficiency, please refer to the related question that you posted on DBA.StackExchange:

Performance and profiling on SELECT * FROM [table] ISNULL([column], '') = '' VS EXIST (SELECT * FROM [table] WHERE [condition])



Related Topics



Leave a reply



Submit