Search code examples
sqlsql-server-2008coalesce

Select another column value from the same table if another column value is NULL


I have a table with four columns: NAME, AGE, PRIMARYWEIGHT and SECONDARYWEIGHT

Where NAME = 'Damian', I wish to select AGE and PRIMARYWEIGHT only if SECONDARYWEIGHT is NULL otherwise I'll take PRIMARYWEIGHT.

Ideally I'd like to give its an alias 'WEIGHT' regardless of whether it was PRIMARYWEIGHT or SECONDARYWEIGHT.


Solution

  • SELECT NAME, AGE, ISNULL(PRIMARYWEIGHT, SECONDARYWEIGH) As WEIGHT 
    

    msdn reference