SUBSTRING_INDEX(), This function returns everything before or after a certain character (or characters) in a string.
This function let you to state the delimiter to use, and you can also state which one (in the event that there’s more than one in the string). Delimiter can be single characters or multiple characters.
Returns the substring from string before count occurrences of the delimiter delim.
If count is positive, everything to the left of the final delimiter (counting from the left) is returned.
If count is negative, everything to the right of the final delimiter (counting from the right) is returned. We can also use a negative(-ve) value for the count. In this case, the beginning of the substring is position characters from the end of the string, rather than the beginning.
SUBSTRING_INDEX() performs a case-sensitive match when searching for delim. See illustrations 3.
A value of 0 for delim returns an empty string. See illustrations 5.
Note : This function is multibyte safe.
SUBSTRING_INDEX(string , delimiter, count );
[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|left|left|left”]
Name, Required /Optional, Value Type, Description
string , Required, String , The input string.
delimiter, Required, String, It will tells delimiter string in the input string..
count, Optional, Numeric , It tells till how many times delimiter should be found, till that match substring will be returned (in the event there are multiple occurrences of the delimiter in the string). It also states whether you want the substring either from the left or right
[/table]
[table caption=”” width=”100%” colwidth=”20%|80%” colalign=”left|left”]
Returns,
String, returns a substring starting from the left or right till the delimiter count is reached.
[/table]
[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, MySQL 4.0
[/table]
Below is an example :
mysql> SELECT SUBSTRING_INDEX('www.tutorialmines.net', '.' ,2) as Output; +-------------------+ | Output | +-------------------+ | www.tutorialmines | +-------------------+ 1 row in set (0.00 sec)
mysql> SELECT SUBSTRING_INDEX('www.tutorialmines.net', '.' ,-2) as Output; +-------------------+ | Output | +-------------------+ | tutorialmines.net | +-------------------+ 1 row in set (0.00 sec)
Below is an example :
mysql> SELECT SUBSTRING_INDEX('mysql and database and php','and', 2) as Output; +---------------------+ | Output | +---------------------+ | mysql and database | +---------------------+ 1 row in set (0.00 sec)
mysql> SELECT SUBSTRING_INDEX('mysql and database and php','and', -2) as Output; +-------------------+ | Output | +-------------------+ | database and php | +-------------------+ 1 row in set (0.00 sec)
Below is an example :
mysql> SELECT SUBSTRING_INDEX('mysql and database and php','AND', 2) as Output; +----------------------------+ | Output | +----------------------------+ | mysql and database and php | +----------------------------+ 1 row in set (0.00 sec)
mysql> SELECT SUBSTRING_INDEX('mysql and database and php','and', 2) as Output; +---------------------+ | Output | +---------------------+ | mysql and database | +---------------------+ 1 row in set (0.00 sec)
It will return NULL if any of the arguments is NULL. Let’s see the below example.
mysql> SELECT SUBSTRING_INDEX('www.tutorialmines.com','.', NULL) as Output1, SUBSTRING_INDEX('www.tutorialmines.com',NULL, 2) as Output2; +---------+---------+ | Output1 | Output2 | +---------+---------+ | NULL | NULL | +---------+---------+ 1 row in set (0.00 sec)
Let’s see the below example.
mysql> SELECT SUBSTRING_INDEX('www.tutorialmines.com','.', 0) as Output1; +---------+ | Output1 | +---------+ | | +---------+ 1 row in set (0.00 sec)
See all MySQL String functions MySQL 8 String Functions.
App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…
As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…
Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…
COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…
Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…
For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…