Skip to content
Advertisement

Update column with value from two potential columns

I have 3 different columns for phone numbers that I need to fix in my table. My goal is to replace phone (if empty or null) with the value of either phone2 or phone 3. I don’t care which one as long as it’s a phone number. I know how to do this with one column (see code below), but cannot figure out how to use an OR statement for the 3rd column. Query and data is below.

Original query

Original data

What I want to see

I’d really appreciate any suggestions. I really want to avoid having to run two different queries

Advertisement

Answer

To deal with null values, you can use coalesce():

This updates phones that are null with phone2 or phone3 (the first non-null column wins).

If you want to avoid empty strings as well as nulls:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement