Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

isnull vs is null

Tags:

I have noticed a number of queries at work and on SO are using limitations in the form:

isnull(name,'') <> '' 

Is there a particular reason why people do that and not the more terse

name is not null 

Is it a legacy or a performance issue?

like image 540
tgandrews Avatar asked Jun 25 '10 13:06

tgandrews


People also ask

Is NULL vs Isnull?

You might confuse between SQL Server ISNULL and IS NULL. We use IS NULL to identify NULL values in a table. For example, if we want to identify records in the employee table with NULL values in the Salary column, we can use IS NULL in where clause.

Is NULL and is not null in same SQL?

The IS NULL condition is satisfied if the column contains a null value or if the expression cannot be evaluated because it contains one or more null values. If you use the IS NOT NULL operator, the condition is satisfied when the operand is column value that is not null, or an expression that does not evaluate to null.

Is NVL and Isnull the same?

ISNULL replaced the Oracle NVL function in the SQL server. When an expression in SQL server is NULL, the ISNULL function allows you to return an alternative value for the null. ISNULL checks whether the value or an expression is true or false.

IS NULL THEN 0 in SQL?

What is a SQL NULL value? In terms of the relational database model, a NULL value indicates an unknown value. If we widen this theoretical explanation, the NULL value points to an unknown value but this unknown value does not equivalent to a zero value or a field that contains spaces.


1 Answers

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.

enter image description here

like image 150
Martin Smith Avatar answered Oct 27 '22 00:10

Martin Smith