Nov 10, 2018

Example: SUBSTRING_INDEX() function in MySQL

 SUBSTRING_INDEX(str,delim,count)

Returns the substring from string str 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. SUBSTRING_INDEX() performs a case-sensitive match when searching for delim.

mysql> SELECT SUBSTRING_INDEX('it.i88.ca', '.', 2);
        -> 'it.i88'
mysql> SELECT SUBSTRING_INDEX('it.i88.ca', '.', -2);
        -> 'i88.ca'

To get domain from email address:

mysql> SELECT SUBSTRING_INDEX('it@i88.ca', '@', -1);
        -> 'i88.ca'

This function is multi-byte safe.