This tutorial explains how to use the MySQL SUBSTRING_INDEX function with syntax and examples.
The MySQL SUBSTRING_INDEX function returns the substring of string before number of occurrences of delimiter.
The syntax for the SUBSTRING_INDEX function in MySQL is:
SUBSTRING_INDEX( string, delimiter, number )
The source string.
The delimiter to search for in string.
The number of times to search for delimiter.
The SUBSTRING_INDEX function can be used in the following versions of MySQL:
Let's look at some MySQL SUBSTRING_INDEX function examples and explore how to use the SUBSTRING_INDEX function in MySQL.
For example:
mysql> SELECT SUBSTRING_INDEX('www.AODBA.com', '.', 1);
Output: 'www'
mysql> SELECT SUBSTRING_INDEX('www.AODBA.com', '.', 2);
Output: 'www.AODBA'
mysql> SELECT SUBSTRING_INDEX('www.AODBA.com', '.', -1);
Output: 'com'
mysql> SELECT SUBSTRING_INDEX('www.AODBA.com', '.', -2);
Output: 'AODBA.com'