- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- 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 split string in MySQL using SUBSTRING_INDEX?
Let us first create a table −
mysql> create table DemoTable1465 -> ( -> Name varchar(40) -> ); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1465 values('Chris Brown'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable1465 values('David Miller'); Query OK, 1 row affected (0.26 sec) mysql> insert into DemoTable1465 values('John Doe'); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1465;
This will produce the following output −
+--------------+ | Name | +--------------+ | Chris Brown | | David Miller | | John Doe | +--------------+ 3 rows in set (0.00 sec)
Following is the query to split string in MySQL using SUBSTRING_INDEX(). Here, we are splitting first and last name strings −
mysql> select -> substring_index(substring_index(Name, ' ', 1), ' ', -1) as StudentFirstName, -> substring_index(substring_index(Name, ' ', 3), ' ', -1) AS StudentLastName -> from DemoTable1465;
This will produce the following output −
+------------------+-----------------+ | StudentFirstName | StudentLastName | +------------------+-----------------+ | Chris | Brown | | David | Miller | | John | Doe | +------------------+-----------------+ 3 rows in set (0.00 sec)
Advertisements