tags:

views:

56

answers:

2

Hi, here in this query I want to replace the values in Person.Contact of Adventureworks database with some new values. The below query case statement is working fine for other values but I am not able to change the values those are in NULL. I am using SQL Server. Any help is appreciated.

select contactid,Title,FirstName,MiddleName,
case MiddleName
when 'R.' then 'Robert'
when 'B.' then 'Bids'
when 'J.' then 'John'
when is null then 'New Name'
else 'No Name'
end, LastName from Person.Contact
+3  A: 
case 
when MiddleName is null then ...
when MiddleName = 'R' then ...
end
GSerg
+2  A: 

I'd use the ISNULL function - it will return the value given if the field is NULL:

select contactid,Title,FirstName,MiddleName,
case ISNULL(MiddleName, 'NULLVALUE')
when 'R.' then 'Robert'
when 'B.' then 'Bids'
when 'J.' then 'John'
when 'NULLVALUE' then 'New Name'
else 'No Name'
end, LastName from Person.Contact
marc_s
Wrong on NULL case.
Johan Buret
@Johan: my fault - fixed it. Now it should work fine.
marc_s
I agree. Let's just hope that nobody has 'NULLVALUE' as middle name.
Johan Buret