tags:

views:

80

answers:

6

I wrote a T-SQL Statement similar like this (the original one looks different but I want to give an easy example here):

SELECT first_name + CASE last_name WHEN null THEN 'Max' ELSE 'Peter' END AS Name FROM dbo.person

This Statement does not have any syntax errors but the case-clause always chooses the ELSE-part - also if the last_name is null. But Why?

What I want to do is to unite first_name and last_name, but if last_name is null the whole name becomes null:

SELECT first_name + CASE last_name WHEN null THEN '' ELSE ' '+last_name END AS Name FROM dbo.person

Do you know where the problem is? Thank you for your help!

+2  A: 
CASE WHEN last_name IS NULL THEN '' ELSE ' '+last_name
Marcelo Cantos
You forgot the `END` at the end. ;-)
Prutswonder
Thank you for your answer but if I do like this I get a syntax error:Msg 156, Level 15, State 1, Line 5Incorrect syntax near the keyword 'THEN'.
meni
Okay sorry I think I made a mistake during testing your answer. That works!! Thank you very much!
meni
If you found that this answered your question click the checkmark next to it to mark it as the answer.
Gage
@Luther's COALESCE suggestion is better than my answer. It's marginally less efficient, but much more elegant.
Marcelo Cantos
+3  A: 

The WHEN part is compared with ==, but you can't really compare with NULL. Try

CASE WHEN last_name is NULL  THEN ... ELSE .. END

instead or COALESCE:

COALESCE(' '+last_name,'')

(' '+last_name is NULL when last_name is NULL, so it should return '' in that case)

Luther Blissett
Okay thank you for the information with the == in the WHEN part. I didn't know that. With COALESCE it does also work fine. Haven't thought that there are so much possibilities to do that.
meni
+2  A: 

Given your query you can also do this:

SELECT first_name + ' ' + ISNULL(last_name, '') AS Name FROM dbo.person
Ian Jacobs
This adds a redundant space when last_name is null, which is what the OP was trying to avoid.
Marcelo Cantos
Better use `ISNULL(' '+ last_name, '')` to prevent the redundant space.
Prutswonder
Yeah realized it after I posted. Figured I'd leave it since it solved the part he was having trouble with.
Ian Jacobs
Thank you! Does also work.
meni
A: 

The problem is that null is not considered equal to itself, hence the clause never matches.

You need to check for null explicitly:

SELECT CASE WHEN last_name is NULL THEN first_name ELSE first_name + ' ' + last_name
b0fh
A: 

try:

SELECT first_name + ISNULL(' '+last_name, '') AS Name FROM dbo.person

This adds the space to the last name, if it is null, the entire space+last name goes to NULL and you only get a first name, otherwise you get a firts+space+last name.

this will work as long as the default setting for concatenation with null strings is set:

SET CONCAT_NULL_YIELDS_NULL ON 

this shouldn't be a concern since the OFF mode is going away in future versions of SQl Server

KM
A: 

There are plenty of solutions but none covers why the original statement doesn´t work.

CASE last_name WHEN null THEN '' ELSE ' '+last_name

After the when, there is a check for a condition, which can be true, false.

If one part of a condition is null, the result of this condition will be false. To avoid this, Coalesce is the best way.

Patrick Säuerl