Substitute one columns results for another

Discussion in 'Databases' started by unitedlocalbands, Sep 20, 2007.

  1. #1
    How do you write the SQL statment so that it will return results from two columns and in the case where one of the two columns has a null record, substitute the column with the result from the other column.

    Simalar to COALESCE but with two columns

    
    
    SELECT COLUMN1, COLUMN2
    FROM TABEL1
    WHERE IDCOLUMN = 'USERID'
    
    
    Code (markup):
    I know this is not right but its the same Idea...

    
    
    SELECT COALESCE(COLUMN1, COLUMN2) AS NEWCOLUMN
    FROM TABEL1
    WHERE IDCOLUMN = 'USERID'
    
    Code (markup):
     
    unitedlocalbands, Sep 20, 2007 IP
  2. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #2
    SELECT c1, IF (c2 IS NULL, c1, c2)
    FROM table1
    ...
     
    krt, Sep 20, 2007 IP