
- 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
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)
- Related Articles
- Get the index of last substring in a given string in MySQL?
- Return the lowest index in the string where substring is found using Python index()
- Return the lowest index in the string where substring is found in a range using Python index()
- How to check index within a string of a specified substring in JSP?
- How to Split String in Java using Regular Expression?
- Return the highest index in the string where substring is found using Python rindex()
- How to find index of last occurrence of a substring in a string in Python?
- How to split a string using regular expressions in C#?
- Golang Program to get the index of the substring in a string
- How to apply Substring() for fields in MySQL to get part of string?
- How to extract certain substring from a string using Java?
- Return the highest index in the string where substring is found in a range using Python rindex()
- How can we extract a substring from a string in MySQL?
- How to split a string in Python
- How to split a string in Java?

Advertisements