
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Split a column in 2 columns using comma as separator - MySQL
For this, you can use substring_index() in MySQL. Let us create a table −
Example
mysql> create table demo79 -> ( -> fullname varchar(50) -> ); Query OK, 0 rows affected (0.64
Insert some records into the table with the help of insert command −
Example
mysql> insert into demo79 values("John,Smith"); Query OK, 1 row affected (0.09 mysql> insert into demo79 values("David,Miller"); Query OK, 1 row affected (0.11 mysql> insert into demo79 values("Chris,Brown"); Query OK, 1 row affected (0.07
Display records from the table using select statement −
Example
mysql> select *from demo79;
This will produce the following output −
Output
+--------------+ | fullname |+--------------+
| John,Smith || David,Miller |
| Chris,Brown |+--------------+
3 rows in set (0.00 sec)
Following is the query to split a column in 2 columns using comma as separator −
Example
mysql> select -> fullname, -> substring_index(fullname, ',', 1) First_Name, -> substring_index(fullname, ',', -1) Last_Name -> from demo79;
This will produce the following output −
Output
| fullname | First_Name | Last_Name |
+--------------+------------+-----------+| John,Smith | John | Smith |
| David,Miller | David | Miller || Chris,Brown | Chris | Brown |
+--------------+------------+-----------+3 rows in set (0.00 sec)
- Related Articles
- How to set comma as decimal separator in R?
- Split the left part of a string by a separator string in MySQL?
- Searching from a comma separated MySQL column?
- How to split a column in MySQL?
- How to split a string column into multiple columns in R?
- Can I search for particular numbers in a MySQL column with comma separated records using a MySQL query?
- Split float value in two columns of a MySQL table?
- Split String with Comma (,) in Java
- Group by one column and display corresponding records from another column with a separator in MySQL
- MySQL select distinct rows into a comma delimited list column?
- Display MySQL Results as comma separated list?
- Find specific records from a column with comma separated values in MySQL
- How to set a comma separated list as a table in MySQL?
- MySQL query to split a column after specific characters?
- How to include quotes in comma separated column with MySQL?

Advertisements