SQL: How can i update a value on a column only if that value is null?

Posted by user321185 on Stack Overflow See other posts from Stack Overflow or by user321185
Published on 2010-04-20T14:35:17Z Indexed on 2010/04/20 14:43 UTC
Read the original article Hit count: 262

Filed under:
|
|
|
|

Hey,

I have an SQL question which may be basic to some but is confusing me. Here is an example of column names for a table 'Person': PersonalID, FirstName, LastName, Car, HairColour, FavDrink, FavFood

Let's say that I input the row: 121312, Rayna, Pieterson, BMW123d, Brown, NULL, NULL

Now I want to update the values for this person, but only if the new value is not null, Update: 121312, Rayna, Pieterson, NULL, Blonde, Fanta, NULL

The new row needs to be: 121312, Rayna, Pieterson, BMW123d, Blonde, Fanta, NULL

So I was thinking something along the lines of: Update Person(PersonalID, FirstName, LastName, Car, HairColour, FavDrink, FavFood) set Car = @Car (where @Car is not null), HairColour = @HairColour (where @HairColour...)... etc.

My only concern is that I can't group all the conditions at the end of the query because it will require all the values to have the same condition. Can't i do something like Update HairColour if @HairColour is not Null

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about mysql