Home > Enterprise >  Merge two columns and create a new column using SQL?
Merge two columns and create a new column using SQL?

Time:11-11

How to merge 2 columns in a view and create new column in pgAdmin.

Like, I have first name and last name in a table, and I want to combine these 2 columns and create a new column called fullname.

Can anyone suggest a workable solution?

CodePudding user response:

Add a computed column to your table. Will always have up-to-date data!

alter table tablename
    add fullname varchar(50) GENERATED ALWAYS AS (firstname || lastname) stored;

Where you specify data type and length as the sum of firstname's and lastname's data types. (E.g. varchar(25) varchar(25) -> varchar(50).)

Edit: Or create a view:

create view viewname as
select t.*, t.firstname || t.lastname as fullname
from tablename t
  • Related