Pandafreak
Pandafreak

Reputation: 107

Dynamic column fields using existing column values in SQL

I have this existing query

Select 
   mt.First_name,
   mt.Last_name as OLD_Last_name,
   ot.Last_name as New_Last_name,
   ot.Date as Update_Date,
from maintable as mt
JOIN othertable as ot on mt.id=ot.id

I'd like to join a new column with the following output: [mt.First_name] [ot.Last_name], nee [mt.Last_name] changed their name on [ot.Date]. I tried using a case statement but didn't get it right.

Upvotes: 0

Views: 95

Answers (1)

Felipe Hoffa
Felipe Hoffa

Reputation: 59315

For closure, moving @Jnevill answer from comment to actual answer:

SELECT mt.First_name || ' ' || ot.Last_name || ', nee ' || mt.last_name || ' changed their name on ' || ot.Date AS yournewcolumn, mt.First_name
  ,    mt.Last_name as OLD_Last_name
  ,    ot.Last_name as New_Last_name
  ,    ot.Date as Update_Date 
from maintable as mt 
JOIN othertable as ot on mt.id=ot.id

Apparently OP wanted to know how to concatenate strings, which is done with ||.

Upvotes: 1

Related Questions