w3resource
MYSQL Tutorial

MySQL SUBSTRING_INDEX() function

SUBSTRING_INDEX() function

MySQL SUBSTRING_INDEX() returns the substring from the given string before a specified number of occurrences of a delimiter.

The substring returned from the left of the final delimiter when the specified number is a positive number and from the right of the final delimiter when the specified number is a negative number.

If the specified number is greater than the number of occurrence of delimiter, the returned substring will be the total string. If the specified number is 0, nothing will be fetched from the given string.

Syntax:

SUBSTRING_INDEX(str, delim, count)

Argument

Name Description
str A string.
delim A delimiter.
count An integer indicating the number of occurrences of delim.

Syntax Diagram:

MySQL SUBSTRING_INDEX() Function - Syntax Diagram

MySQL Version: 5.6

Video Presentation

Example: MySQL SUBSTRING_INDEX() function

The following MySQL statement returns the substring form the left of the final delimiter i.e. 2nd delimiter (.) from the given string ‘www.mytestpage.info’. Counting starts from the left of the string.

Code:

SELECT SUBSTRING_INDEX('www.mytestpage.info','.',2);

Sample Output:

mysql> SELECT SUBSTRING_INDEX('www.mytestpage.info','.',2);
+----------------------------------------------+
| SUBSTRING_INDEX('www.mytestpage.info','.',2) |
+----------------------------------------------+
| www.mytestpage                               | 
+----------------------------------------------+
1 row in set (0.02 sec)

Pictorial Presentation

MySQL SUBSTRING_INDEX() with positive value

Example of MySQL SUBSTRING_INDEX() function using negative count

The following MySQL statement returns the substring from the right of the final delimiter i.e. 2nd delimiter (.) from the given string ‘www.mytestpage.info’. Counting starts from the right of the string.

Code:

SELECT SUBSTRING_INDEX('www.mytestpage.info','.',-2); 

Sample Output:

mysql> SELECT SUBSTRING_INDEX('www.mytestpage.info','.',-2); 
+-----------------------------------------------+
| SUBSTRING_INDEX('www.mytestpage.info','.',-2) |
+-----------------------------------------------+
| mytestpage.info                               | 
+-----------------------------------------------+
1 row in set (0.00 sec)

Pictorial Presentation

MySQL SUBSTRING_INDEX() with negative value

Example: Split an IP address into 4 respective octets using MySQL SUBSTRING_INDEX() function

The following MySQL command splits an IP address into 4 respecting octets (unit of digital information). Assume the IP addresses are stored in a sample table called 'log_file'.

mysql> SELECT ip, SUBSTRING_INDEX(ip,'.',1) AS part1, 
SUBSTRING_INDEX(SUBSTRING_INDEX(ip,'.',2),'.',-1) AS part2, 
SUBSTRING_INDEX(SUBSTRING_INDEX(ip,'.',-2),'.',1) AS part3, 
SUBSTRING_INDEX(ip,'.',-1) AS part4  FROM log_file;
+-----------------+-------+-------+-------+-------+
| ip              | part1 | part2 | part3 | part4 |
+-----------------+-------+-------+-------+-------+
| 127.0.0.1       | 127   | 0     | 0     | 1     |
| 192.128.0.15    | 192   | 128   | 0     | 15    |
| 255.255.255.255 | 255   | 255   | 255   | 255   |
+-----------------+-------+-------+-------+-------+
3 rows in set (0.00 sec)

All String Functions

MySQL String Functions, slide presentation

Many of our valued users post comments along with piece of code. Disqus may, false positively, consider those piece of code as Bad or Starnge syntax and send those comments to spam automatically. Even if that happens, we will make sure those useful comments are taken out of spam and approved. It may take a couple of days for that though, but any useful comment will be brought to public view for sure. We regret if that happend to any user. You may write us directly regarding this to - w3resource[at]yahoo[dot]com.