Add new column in postgresql - sql

I'm new to postgresql. I have a table with first_names and last_names. I want to add a column called full_name in the table (which would add the first_name and last_name). To add a column I used the following code:
ALTER TABLE actor ADD COLUMN full_name varchar(50);
To concatenate the the column, I can use the formula:
CONCAT(first_name,' ',last_name) from actor
But I do not know as how to add the concat values to my new full_name column. Can someone help?

You can update the column:
update actor
set full_name = CONCAT(first_name, ' ', last_name);

Related

Can I map a new column created from my custom SQL query to entity class field?

I have a requirement where I am adding new column from a select query like this:
select
sy.FIRST_NAME || sy.LAST_NAME as full_name,
e.*
from
employee e
left join
USERS sy on sy.SY_USER_ID = act.SY_USER_ID;
I have all the columns of the employee table in my entity class as fields - except for full_name.
How to map this full_name column to a field in my entity class?
I believe you have columns first_name and last_name as you use to create full_name column. Therefore, you already mapped the full_name column to align with all previous columns in the table. Try run your SQL.

inserting multiples values in one column

I have a question about SQL. I have created a table in SQL with only one column containing the name of two people (say John and Matt). Later I added a new column into the table with ALTER TABLE. This column will contain the surname of these people.
My question is, in case mmy table contained several people already is there a command to enter the surnames for all the people at once rather than writing one command for each person as in:
INSERT INTO table (Surname) VALUE (John's surname) and
INSERT INTO table (Surname) VALUE (Matt's surname) ?
Thanks in advance
P.D.
I tried something like:
UPDATE foo set Surname=("Parker","Walker") where Name =("John","Matt") but does not work
You want an update. Something like this:
update t
set surname = 'John'' surname'
where firstname = 'John';
You can do this separately for each name. Or use a case expression for multiple ones:
UPDATE foo
SET Surname = (CASE WHEN Name = 'John' THEN 'Parker'
WHEN Name = 'Matt' THEN 'Walker'
END)
WHERE Name IN ('John', 'Matt');

Append & populate first name, last name columns using full name column in same table?

I'm using SQL Server and I have a column called "Full Name" that I pulled from a separate table. I have a column for First Name & Last name (among many other columns).
Here is the code that I found and used and it worked:
SELECT Full_Name,
LEFT(Full_Name,CHARINDEX(' ',Full_Name )-1) AS First_Name,
RIGHT(Full_Name, LEN(Full_Name)- CHARINDEX(' ', Full_Name)) AS Last_Name
FROM table_name
That extracted the name, but upon viewing the entire table using this command:
SELECT * FROM table_name
I don't see it? Is there a method that I can use to insert the data into the First_name and Last_name columns without adding any additional Rows
Thank you !
Perhaps creating a view wil be sufficient:
CREATE VIEW table_name_v
SELECT Full_Name,
LEFT(Full_Name,CHARINDEX(' ',Full_Name )-1) AS First_Name,
RIGHT(Full_Name, LEN(Full_Name)- CHARINDEX(' ', Full_Name)) AS Last_Name
FROM table_name
Then instead of using the table name in subsequent queries you use the view instead
SELECT * FROM table_name_v
The problem you face otherwise is that if you add first and last name columns to this table that you now end-up with 3 columns to maintain, or that you may need to replace the data entry screen(s) that use full_name to start using first and last name columns instead.
If you really do want to proceed by adding the columns, you could try using "computed columns" which would avoid the necessity to change data entry screens etc.
CREATE TABLE mytable(
full_name VARCHAR(15) NOT NULL
);
INSERT INTO mytable(full_name) VALUES ('fred flintstone');
select * from mytable
full_name
fred flintstone
alter table mytable
add
first_name as LEFT(Full_Name,CHARINDEX(' ',Full_Name )-1)
, Last_Name as RIGHT(Full_Name, LEN(Full_Name)- CHARINDEX(' ', Full_Name))
select * from mytable
full_name
first_name
Last_Name
fred flintstone
fred
flintstone
db<>fiddle for computed columns here
If you realy do want to store these 2 separate columns (rather than computing them) then you need to add the columns to your table and run an update statement to populate them as follows:
alter table mytable
add
first_name varchar(100)
, last_name varchar(100)
update mytable
set
first_name = LEFT(Full_Name,CHARINDEX(' ',Full_Name )-1)
, Last_Name = RIGHT(Full_Name, LEN(Full_Name)- CHARINDEX(' ', Full_Name))
db<>fiddle for adding columns and updating here

How to split name column into firstName and lastName columns in PostgreSQL?

Assume this User table in PostgreSQL:
CREATE TABLE "User" (
id SERIAL PRIMARY KEY,
email text NOT NULL,
name text,
);
I want to split name into firstName and lastName and update the existing rows accordingly. I understand that I can use the following SQL to return the data as firstName and lastName:
SELECT
split_part("name", ' ', 1) AS "firstName",
split_part("name", ' ', 2) AS "lastName"
FROM "User";
I guess I now need to use UPDATE somehow in order to update the existing rows, but I'm not clear on the exact syntax.
One straightforward way to do this would be:
UPDATE "User"
SET "firstName" = split_part("name", ' ', 1),
"lastName" = split_part("name", ' ', 2)
(No WHERE clause because I'm assuming you want to transform the whole table that way.)

Filling a new SQL column using two other columns

I am trying to combine to rows in a table into one new row in a table. Specifically columns First_Name and Last_name into First_Last. I am having trouble getting my query to run due to the fact I am trying to do this for all entries in the table and not just 1 row. Any suggestions?
Current Code:
update Name
set First_Last = (select First_Name + ' ' + Last_Name from Name)
Thanks,
Justin
Assuming you intend updating all rows on the same table using the First_Name and Last_Name values for each row, it is as simple as (in Sql Server):
update MyTable
set First_Last = First_Name + ' ' + Last_Name;
In most other RBDMS, you use the pipe || operator or the CONCAT function to combine text.
Note that most RDBMS also have the concept of a computed (sometimes called generated) column, which prevents the redundancy and synchronisation issues with storing a derived field:
CREATE TABLE MyTable...
(
First_Last AS first_name + ' ' + last_name
);
You did not specify your RDBMS but if you are using MySQL you can use the CONCAT() function
update Name
set First_Last = concat(First_Name, ' ', Last_Name)