![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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 |
#3
| |||
| |||
|
|
Why can I do: SELECT PersonID = CASE o.ID WHEN NULL THEN o2.ID ELSE o.ID END |
#4
| |||
| |||
|
|
SELECT PersonID = CASE o.ID WHEN NULL THEN o2.ID ELSE o.ID END |
|
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 |
#5
| |||
| |||
|
|
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) |
| 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 |
#6
| |||
| |||
|
|
"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 |
#7
| |||
| |||
|
|
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 |
#8
| |||
| |||
|
|
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. ^^^^^^ |
|
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. |
#9
| |||
| |||
|
|
Not the adjective I would have picked. |
|
Is there a technical difference that matters? |
|
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. |
#10
| |||
| |||
|
|
Gene Wirchenko (genew (AT) ocis (DOT) net) writes: Not the adjective I would have picked. You're not Swedish, are you? :-) |
|
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. |
![]() |
| Thread Tools | |
| Display Modes | |
| |