Update column to another column using condition

  • I have a table that looks like the following:

    I want to be able to update the trendValue column based on whether or not the other columns are null. The kicker is, I want it to select the column that is used to update the trend value in this order:

    1. If prev15 is not null, use this

    2. If prev30 is not null, use this

    3. If prev45 is not null, use this

    4. If prev60 is not null, use this

    5. If they are all null, leave as null

    I know it would be easy to do with a cursor, but is there any other way?

    Thanks,

  • You don't need a cursor at all. Use COALESCE function:

    UPDATE myTable SET RequiredColumn = COALESCE(prev15, prev30, prev45, prev60)

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

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

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