
- 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
What is the use of MySQL SUBSTRING_INDEX() function?
MySQL SUBSTRING_INDEX() function returns the substring from a given string before the occurrences of a delimiter. The number of occurrences is specified as an argument of it. It would be more understood with the help of syntax of SUBSTRING_INDEX() function as follows −
Syntax
SUBSTRING_INDEX(Str, delim, count)
Here,
- Str is the string from which it returns the substring.
- Delim is the delimiter which is to be search in string.
- Count is the number of times the delimiter is to be searched.
Example
mysql> Select SUBSTRING_INDEX('My Name is Ram','a',2); +-----------------------------------------+ | SUBSTRING_INDEX('My Name is Ram','a',2) | +-----------------------------------------+ | My Name is R | +-----------------------------------------+ 1 row in set (0.00 sec)
The above query returns the substring before the occurrence of second ‘a’ because the delimiter was ‘a’ and count was 2.
mysql> Select SUBSTRING_INDEX('My Name is Ram','a',1); +-----------------------------------------+ | SUBSTRING_INDEX('My Name is Ram','a',1) | +-----------------------------------------+ | My N | +-----------------------------------------+ 1 row in set (0.00 sec)
The above query returns the substring before the occurrence of first ‘a’ because the delimiter was ‘a’ and count was 1.
- Related Articles
- What is the use of MySQL FROM_UNIXTIME() function?
- What is the use of MySQL GET_FORMAT() function?
- What is the use of MySQL LAST_INSERT_ID() function?
- What is the use of MySQL CONCAT_WS() function?
- What is the use of MySQL TRUNCATE() function?
- What is the use of MySQL UNHEX() function?
- What is the use of MySQL CHAR() function?
- What is the use of FIND_IN_SET () function in MySQL?
- What is the use of LOCATE() function in MySQL?
- What is the use of MySQL IFNULL() control flow function?
- What is the use of MySQL NULLIF() control flow function?
- 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()
- What is the use of JavaScript eval function?
- What is the use of Math.hypot() function in JavaScript?

Advertisements