adding a column to a table on condition

  • Hi, I have a table let's call it Person. This table have many column, among which are 3 that I need to check their values.

    1-Cell number

    2-Home number

    3-Work number

    For each record, only one of these columns have a value, the other 2 are null.

    I need to add an extra column to this table, let's call it contact number, and copy the value of the existing phone number in each row to this column. How do I do that? I am a true newbi. Some code is appreciated.

    Thanks.

  • new coder (10/30/2008)


    Hi, I have a table let's call it Person. This table have many column, among which are 3 that I need to check their values.

    1-Cell number

    2-Home number

    3-Work number

    For each record, only one of these columns have a value, the other 2 are null.

    I need to add an extra column to this table, let's call it contact number, and copy the value of the existing phone number in each row to this column. How do I do that? I am a true newbi. Some code is appreciated.

    Thanks.

    Assuming you have already added the column, ContactNumber, here is the code:

    update dbo.Person set

    ContactNumber = coalesce(CellNumber,HomeNumber,WorkNumber);

  • Thanks.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply