0

I tried googling this I'm not exactly sure how to formulate my search. I searched for merge, combining, replacing. Any other suggestions or advice are welcome. The results I find don't replace the field of column A but either adds both values/strings like colA field 'John'+" "+'Smith'. This is not what I'm looking for. i found someone else with the same question but without the right answer here

The concat function is not what I'm looking for since it only adds the values and not replace them. (Not sure in which versions of SQL it works)

Have this:

table1
        id  row text
row1    1   1   AAA     
row2    1   2   BBB     
row3    1   3   CCC     
row4    1   4   DDD     
row5    2   1   aaa
row6    2   2   bbb

table2
        id  row text    
row1    1   1   AAA     
row2    1   3   CCCCC       
row3    2   1   aaa     
row4    2   2   bbbbb

Desired result:

Display
        id  row text
row1    1   1   AAA
row2    1   2   BBB
row3    1   3   CCCCC
row4    1   4   DDD
row5    2   1   aaa
row6    2   2   bbbbb

I don't want to modify the original records and table designs.

Thank you in advance.

0

1 Answer 1

1
SELECT table1.id
     , table1.row
     , Coalesce(table2.text, table1.text) As text
FROM   table1
 LEFT
  JOIN table2
    ON table2.id  = table1.id
   AND table2.row = table1.row

The Coalesce() function takes a minimum of two parameters and returns the first non-null value from those values.

Also note that we're performing an outer join so that we have all values from table1 and where we can, we match with values from table2. If you were to change it to SELECT * ... then it may be more illustrative.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.