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

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.)

Related

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

Updating with a select one table

I want to update a column with a concatenation of other columns within the same table.
This is for customer names in a table. There are separate columns for "Title" i.e. Mr, Ms, Mrs etc, "FirstName", "MiddleName" and "LastName". I altered the table by adding a new "FullName" column, which I tried to fill with a concatenation of the former columns.
SET [SalesLT].[Customer].[FullName] = (SELECT
Title,
FirstName,
MiddleName,
LastName,
CONCAT(Title,' ',FirstName,' ',MiddleName,' ',LastName) as FullName
FROM [AdventureWorksLT2008R2].[SalesLT].[Customer])
WHERE FullName = NULL;
I'm getting this
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Since you are updating a column value based on other columns in the same table, you not select them again.
UPDATE [SalesLT].[Customer]
SET [SalesLT].[Customer].[FullName] =
CONCAT(Title,' ',FirstName,' ',MiddleName,' ',LastName)
WHERE FullName is NULL;
Note: I did not change your where clause condition, neither concat function. In some databases you may need to do FullName is NULL instead of FullName = NULL. And in some databases, you may need to concat multiple values with ||.

Matching tuples in sql

I am trying to write a query that matches a tuple of columns from a list. So let's say I have list of first names and last names and I want to match the combination of first and last name of the same index from the database.
first_names = ["Joe", "Freddy", "Michael"]
last_names = ["Jason", "Kruger", "Myers"]
In this case I want query to return some other column for a record that has either name "Joe Jason", "Freddy Kruger" or "Michael Myers".
For me, the obvious way is to group by first and last name and use group concat then match against concatenated field. But I want to try to avoid that. Is there anyway matching by tuples can be done in SQL?
Some databases allow you to express this logic as:
where (firstname, lastname) in ( ('Joe', 'Jason'), ('Freddy', 'Kruger'), ('Michael, 'Myers') )
In all, you can express this using boolean logic:
where (firstname = 'Joe' and lastname = 'Jason') or
(firstname = 'Freddy' and lastname = 'Kruger') or
(firstname = 'Michael' and lastname = 'Myers')

Add new column in postgresql

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);

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)