What does the equals one mean - ISNULL (something, 1) = 1

Viewed 1353

I have the following code.

.....
.....
WHERE 0 = 0 
AND Isnull(something, 1) = 1
....
....

I know that isnull function looks at something and if it is a null replace it with 1. But what does the =1 stand for exactly, what does it mean ?

I'm working in Microsoft SQL Server and the code is Coldfusion.

4 Answers

Your query Isnull(something, 1) = 1 is saying to return something which has only null value with 1.

I would re-write it as to make it Sargable :

WHERE (something IS NULL OR something = 1)

This is equivalent to:

WHERE something IS NULL or something  = 1;

because 0=0 is always true.

it will replace the value of something (column name For a row) if it is null with 1 and then check if the output is one. basically, it is checking if the value is null.

The core of it is that if the value (something) that is null-checked is null or exactly 1 then the expression evaluates as true.

If something equals null then the result of ISNULL(p1, p2) evaluates to the value of p2. If something equals anything other than null then it evaluates to the value of p1. Then the expression evaluates against the result of ISNULL

p1     p2  ISNULL-result  expression  result
null   1   1              = 1         true
1      1   1              = 1         true
2      1   2              = 1         false
1      1   1              = 2         false
null   2   2              = 2         true
Related