dbTalk Databases Forums  

Case with NULL

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Case with NULL in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
tshad
 
Posts: n/a

Default Case with NULL - 01-12-2011 , 09:12 PM






Why can I do:

SELECT PersonID = CASE WHEN o.ID IS NULL THEN o2.ID ELSE o.ID END

SELECT PersonID = CASE WHEN o.ID IS NOT NULL THEN o2.ID ELSE o.ID END

SELECT PersonID = CASE o.ID WHEN NULL THEN o2.ID ELSE o.ID END

But not:

SELECT PersonID = CASE o.ID WHEN NOT NULL THEN o2.ID ELSE o.ID END

I get an error:

Incorrect syntax near the keyword 'NOT'.

and

SELECT PersonID = CASE o.ID WHEN IS NOT NULL THEN o2.ID ELSE o.ID END

I get an error:

Incorrect syntax near the keyword 'IS'.

Using this syntax, can I not test for 'NOT NULL'?

Thanks,

Tom

Reply With Quote
  #2  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: Case with NULL - 01-13-2011 , 02:15 AM






On 2011-01-13 04:12, tshad wrote:
[...]
Quote:
SELECT PersonID = CASE o.ID WHEN NULL THEN o2.ID ELSE o.ID END
I'm a bit surprised that this works. This case construction uses "=" and
nothing (including null) equals null. FWIW, this should be equal to:

SELECT coalesce(o.ID, o2.ID)

Quote:
But not:

SELECT PersonID = CASE o.ID WHEN NOT NULL THEN o2.ID ELSE o.ID END

I get an error:

Incorrect syntax near the keyword 'NOT'.

and

SELECT PersonID = CASE o.ID WHEN IS NOT NULL THEN o2.ID ELSE o.ID END

There is another CASE construction (my windows vm is down so I can't
verify whether it is supported by sqlserver)

CASE WHEN o.ID IS NOT NULL THEN o2.ID ELSE ...


/Lennart

Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Case with NULL - 01-13-2011 , 02:49 AM



tshad (tfs (AT) dslextreme (DOT) com) writes:
Quote:
Why can I do:
SELECT PersonID = CASE o.ID WHEN NULL THEN o2.ID ELSE o.ID END
Just because you can, does not mean that you should. This experssion
will always return o.ID, because comparisons will NULL always yield
UNKNOWN.

You should write this as

CASE WHEN o.ID IS NULL THEN o2.ID ELSE o.ID END




--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx

Reply With Quote
  #4  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: Case with NULL - 01-13-2011 , 04:25 AM



Hi Tom,

The CASE expression has two syntaxes:

1:
CASE
WHEN <logic expression> THEN <scalar expression>
{ WHEN <logic expression> THEN <scalar expression> ... }
[ ELSE <scalar expression> ]
END

2:
CASE <scalar expression A>
WHEN <scalar expression> THEN <scalar expression>
{ WHEN <scalar expression> THEN <scalar expression> ... }
[ ELSE <scalar expression> ]
END

The second is defined to be equivalent to the former, where each WHEN
<scalr expression> is replaced by WHEN <scalar expression A> = <scalar
expression>.

Quote:
SELECT PersonID = CASE o.ID WHEN NULL THEN o2.ID ELSE o.ID END
As a result, the expression above is valid syntax (both o.ID and NULL
are valid scalar expressions) - but nonsense. A comparison with NULL
will never evaluate to True, so this whole fragment is logically
equivalent to
SELECT o.ID

Quote:
SELECT PersonID = CASE o.ID WHEN NOT NULL THEN o2.ID ELSE o.ID END
and
SELECT PersonID = CASE o.ID WHEN IS NOT NULL THEN o2.ID ELSE o.ID END
These are both invalid syntax, because neither NOT NULL nor IS NOT
NULL is a valid scalar expression.
--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis

Reply With Quote
  #5  
Old   
tshad
 
Posts: n/a

Default Re: Case with NULL - 01-13-2011 , 12:54 PM



"Lennart Jonsson" <erik.lennart.jonsson (AT) gmail (DOT) com> wrote

Quote:
On 2011-01-13 04:12, tshad wrote:
[...]

SELECT PersonID = CASE o.ID WHEN NULL THEN o2.ID ELSE o.ID END

I'm a bit surprised that this works. This case construction uses "=" and
nothing (including null) equals null. FWIW, this should be equal to:

SELECT coalesce(o.ID, o2.ID)
Is one better than the other?

SELECT PersonID = coalesce(o.ID, o2.ID)

or

SELECT PersonID = CASE WHEN o.ID IS NULL THEN o2.ID ELSE o.ID END

Thanks,

Tom

Quote:

But not:

SELECT PersonID = CASE o.ID WHEN NOT NULL THEN o2.ID ELSE o.ID END

I get an error:

Incorrect syntax near the keyword 'NOT'.

and

SELECT PersonID = CASE o.ID WHEN IS NOT NULL THEN o2.ID ELSE o.ID END


There is another CASE construction (my windows vm is down so I can't
verify whether it is supported by sqlserver)

CASE WHEN o.ID IS NOT NULL THEN o2.ID ELSE ...


/Lennart


Reply With Quote
  #6  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: Case with NULL - 01-13-2011 , 01:09 PM



On 2011-01-13 19:54, tshad wrote:
Quote:
"Lennart Jonsson" <erik.lennart.jonsson (AT) gmail (DOT) com> wrote in message
news:igmca7$b1b$1 (AT) news (DOT) eternal-september.org...
On 2011-01-13 04:12, tshad wrote:
[...]

SELECT PersonID = CASE o.ID WHEN NULL THEN o2.ID ELSE o.ID END

I'm a bit surprised that this works. This case construction uses "=" and
nothing (including null) equals null. FWIW, this should be equal to:

SELECT coalesce(o.ID, o2.ID)

Is one better than the other?

SELECT PersonID = coalesce(o.ID, o2.ID)

or

SELECT PersonID = CASE WHEN o.ID IS NULL THEN o2.ID ELSE o.ID END

I prefer coalesce over case because - IMO - it is easier to read, not to
mention that there is less typing involved. Other than that I don't
think [1] there are any differences.

[1] I have about 1 day experience with sql server, so there might be a
thing or two that I haven't discovered yet ;-)


/Lennart

Reply With Quote
  #7  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Case with NULL - 01-13-2011 , 03:49 PM



tshad (tfs (AT) dslextreme (DOT) com) writes:
Quote:
Is one better than the other?

SELECT PersonID = coalesce(o.ID, o2.ID)

or

SELECT PersonID = CASE WHEN o.ID IS NULL THEN o2.ID ELSE o.ID END
As Lennart says, the former is shorter to type. The algebrizer
rewrites coalesce to a CASE expression, so they are very equivalent.

But you could claim that CASE is better, because it is more honest.
Say that you write:

coalesce((SELECT ....), 4711)

Looks good. But this is the true story:

CASE WHEN (SELECT ...) IS NOT NULL THEN (SELECT ...) ELSE 4711 END

Personally, I use coalesce all the time.

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx

Reply With Quote
  #8  
Old   
Gene Wirchenko
 
Posts: n/a

Default Re: Case with NULL - 01-14-2011 , 02:07 PM



On Thu, 13 Jan 2011 22:49:06 +0100, Erland Sommarskog
<esquel (AT) sommarskog (DOT) se> wrote:

Quote:
tshad (tfs (AT) dslextreme (DOT) com) writes:
Is one better than the other?

SELECT PersonID = coalesce(o.ID, o2.ID)

or

SELECT PersonID = CASE WHEN o.ID IS NULL THEN o2.ID ELSE o.ID END

As Lennart says, the former is shorter to type. The algebrizer
rewrites coalesce to a CASE expression, so they are very equivalent.

But you could claim that CASE is better, because it is more honest.
^^^^^^
Not the adjective I would have picked. Is there a technical
difference that matters?

Quote:
Say that you write:

coalesce((SELECT ....), 4711)

Looks good. But this is the true story:

CASE WHEN (SELECT ...) IS NOT NULL THEN (SELECT ...) ELSE 4711 END

Personally, I use coalesce all the time.
Get the computer to do your lying for you? <BEG>

Sincerely,

Gene Wirchenko

Reply With Quote
  #9  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Case with NULL - 01-14-2011 , 04:13 PM



Gene Wirchenko (genew (AT) ocis (DOT) net) writes:
Quote:
Not the adjective I would have picked.
You're not Swedish, are you? :-)

Quote:
Is there a technical difference that matters?
Well, it was the one I used:

Quote:
Say that you write:

coalesce((SELECT ....), 4711)

Looks good. But this is the true story:

CASE WHEN (SELECT ...) IS NOT NULL THEN (SELECT ...) ELSE 4711 END

Personally, I use coalesce all the time.
When you use CASE, you easier realise that the subquery is computed
twice.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx

Reply With Quote
  #10  
Old   
Gene Wirchenko
 
Posts: n/a

Default Re: Case with NULL - 01-16-2011 , 10:10 PM



On Fri, 14 Jan 2011 23:13:32 +0100, Erland Sommarskog
<esquel (AT) sommarskog (DOT) se> wrote:

Quote:
Gene Wirchenko (genew (AT) ocis (DOT) net) writes:
Not the adjective I would have picked.

You're not Swedish, are you? :-)
No, why?

Quote:
Is there a technical difference that matters?

Well, it was the one I used:

Say that you write:

coalesce((SELECT ....), 4711)

Looks good. But this is the true story:

CASE WHEN (SELECT ...) IS NOT NULL THEN (SELECT ...) ELSE 4711 END

Personally, I use coalesce all the time.

When you use CASE, you easier realise that the subquery is computed
twice.
And only once with coalesce() which would argue in favour of
using it. (Naturally, all bets are off when optimisation enters the
picture.)

Sincerely,

Gene Wirchenko

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.