- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to separate last name and first names in single column into two new columns in MySQL?
For this, use SUBSTRING_INDEX() and REPLACE(). Let us first create a table −
mysql> create table DemoTable (Name varchar(100)); Query OK, 0 rows affected (0.53 sec)
Insert some records in the table using insert command. Here, we have inserted last name and first names −
mysql> insert into DemoTable values('Chris | Bob Brown'); Query OK, 1 row affected (0.17 sec) mysql> insert into DemoTable values('Carol | Robert Taylor'); Query OK, 1 row affected (0.23 sec) mysql> insert into DemoTable values('Sam | David Miller'); Query OK, 1 row affected (0.13 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-----------------------+ | Name | +-----------------------+ | Chris | Bob Brown | | Carol | Robert Taylor | | Sam | David Miller | +-----------------------+ 3 rows in set (0.00 sec)
Following is the query to separate last name and first names into two new columns in MySQL −
mysql> SELECT REPLACE(Name, SUBSTRING_INDEX(Name, ' ', -1),'') AS FirstName, SUBSTRING_INDEX(Name, ' ', -1) AS LastName from DemoTable;
This will produce the following output −
+-----------------+----------+ | FirstName | LastName | +-----------------+----------+ | Chris | Bob | Brown | | Carol | Robert | Taylor | | Sam | David | Miller | +-----------------+----------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL query to display columns name first name, last name as full name in a single column?
- How to create separate columns for first and last name in R?
- Concatenate date and time from separate columns into a single column in MySQL
- Select distinct names from two columns in MySQL and display the result in a single column
- MySQL query to combine two columns in a single column?
- Concatenate all the columns in a single new column with MySQL
- How to get the difference between two columns in a new column in MySQL?
- How to separate two values in single column in R data frame?
- How to convert a matrix to a data frame with column names and row names as new columns in R?
- How to separate two values in single column in data.table object in R?
- Multiply values of two columns and display it a new column in MySQL?
- How to convert multiple columns in an R data frame into a single numerical column along with a column having column names as factor?
- MySQL query to get the length of all columns and display the result in a single new column?
- Divide numbers from two columns and display result in a new column with MySQL
- How to convert multiple columns into single column in an R data frame?

Advertisements