0

I want to update my column by joining both column to one.

FirstName Name
John Kowalsky
Adam Smith
null Bush

I would like to achieve such an effect in column

"Name

John Kowalsky
Adam Smith
Bush

I tried subselect but dont work

Czarnulka
  • 1
  • 1
  • which query did you try? Can you include it in your question? Also maybe related: https://stackoverflow.com/questions/19942824/how-to-concatenate-columns-in-a-postgres-select – jmunsch Nov 17 '22 at 19:23

1 Answers1

1

No sub-select required:

update the_table
   set name = concat_ws(' ', firstname, name);

But updating the column this way is a bad idea as you lose the real (last) name.

I would put this into a view:

create view the_table_with_full_name
as
select name, firstname, concat_ws(' ', firstname, name) as full_name
from the_table;

concat_ws() will properly deal with nulls or empty strings

  • But i need to update column values. In next step i decided to remove whole FirstName column – Czarnulka Nov 17 '22 at 19:33
  • Removing the firstname column is an even worse idea. –  Nov 17 '22 at 20:35
  • why? I dont need anymore this column – Czarnulka Nov 17 '22 at 20:42
  • You want to store the firstname and the lastname of a person. Those should be in **two** columns, not munged together in one. This one of the most basic rules of good database design. –  Nov 17 '22 at 20:50