views:

172

answers:

6

I have the following SQL query:

select AuditStatusId 
from dbo.ABC_AuditStatus 
where coalesce(AuditFrequency, 0) <> 0

I'm struggling a bit to understand it. It looks pretty simple, and I know what the coalesce operator does (more or less), but dont' seem to get the MEANING.

Without knowing anymore information except the query above, what do you think it means?

+13  A: 
select AuditStatusId 
from dbo.ABC_AuditStatus 
where AuditFrequency  <> 0 and AuditFrequency  is not null

Note that the use of Coalesce means that it will not be possible to use an index properly to satisfy this query.

Martin Smith
And you can remove `and AuditFrquency is not null` in most versions of SQL, since `AuditFrequency <> 0` will return unknown in the case of `null`. And rows where the `where` clause evaluates to unkown will not be returned. (The only exception I know of is SQL Server which allows a mode where `null=null` is true. That is not the default mode.)
Shannon Severance
@Shannon - Good point it is superfluous though perhaps clearer.
Martin Smith
Thnx guys. Didn't think of it in that way. Still studying the answers, and applying them to my context to see if I understand.
Saajid Ismail
@Saajid For completeness I should probably say that the `<>` means that you are likely to end up with an index scan rather than an index seek in any event *in this specific case* (unless you have a very high proportion of records that are either NULL or zero) but it is good practice to avoid this kind of syntax as there is no possible upside to doing it from a performance point of view and a definite possible downside.
Martin Smith
Thanx Martin. Your explanantion is a bit beyond me, but I will keep it mind.
Saajid Ismail
A: 

It looks like it's designed to detect a null AuditFrequency as zero and thus hide those rows.

Kalium
A: 

From what I can see, it checks for fields that aren't 0 or null.

Ikke
+5  A: 

COALESCE is the ANSI standard function to deal with NULL values, by returning the first non-NULL value based on the comma delimited list. This:

WHERE COALESCE(AuditFrequency, 0) != 0

..means that if the AuditFrequency column is NULL, convert the value to be zero instead. Otherwise, the AuditFrequency value is returned.

Since the comparison is to not return rows where the AuditFrequency column value is zero, rows where AuditFrequency is NULL will also be ignored by the query.

OMG Ponies
A: 

I think it is more accurately described by this:

select AuditStatusId 
from dbo.ABC_AuditStatus 
where (AuditFrequency IS NOT NULL AND AuditFrequency != 0) OR 0 != 0

I'll admit the last part will never do anything and maybe i'm just being pedantic but to me this more accurately describes your query.

Abe Miessler
A: 

The idea is that it is desireable to express a single search condition using a single expression but it's merely style, a question of taste:


One expression:

WHERE age = COALESCE(@parameter_value, age);

Two expressions:

WHERE (
       age = @parameter_value
       OR
       @parameter_value IS NULL
      );

Here's another example:


One expression:

WHERE age BETWEEN 18 AND 65;

Two expressions

WHERE (
       age >= 18
       AND 
       age <= 65
      );

Personally, I have a strong personal perference for single expressions and find them easier to read... if I am familiar with the pattern used ;) Whether they perform differently is another matter...

onedaywhen