Last index of a given substring in MySQL

MysqlStringLastindexof

Mysql Problem Overview


We can find the index of the first occurrence of a given substring in MySQL using the INSTR() function as follows.

SELECT instr('Have_a_good_day', '_') AS index_position

It would display 5, the first occurrence of the specified substring which is in this case an underscore _.

I need to obtain the last occurrence of a given character (or a substring) something like the Java lastIndexOf(String str) method of the String class but I can't find any built-in function in MySQL.

Is there any built-in functionality to achieve this in MySQL?

Mysql Solutions


Solution 1 - Mysql

@Marc B was close. In MySQL, following statement returns 12:

SELECT CHAR_LENGTH("Have_a_good_day") - LOCATE('_', REVERSE("Have_a_good_day"))+1;

Anticipating a possible use of the value, the following statement extracts the left part of the string before the last underscore(i.e., _):

SELECT LEFT("first_middle_last", CHAR_LENGTH("first_middle_last") - LOCATE('_', REVERSE("first_middle_last")));

The result is "first_middle". If you want to include the delimiter, use:

SELECT LEFT("first_middle_last", CHAR_LENGTH("first_middle_last") - LOCATE('_', REVERSE("first_middle_last"))+1);

It would be nice if they enhanced LOCATE to have an option to start the search from the right.

If you want the right part of the string after the last space a better solution is:

SELECT SUBSTRING_INDEX("first_middle_last", '_', -1);

This returns "last".

Solution 2 - Mysql

If you don't want the overhead of REVERSE use the following:

LEFT
(
   'Have_a_good_day', 
   LENGTH('Have_a_good_day') - LENGTH(SUBSTRING_INDEX('Have_a_good_day','_',-1))-1
)

Solution 3 - Mysql

I think you can use substring_index in this way:

select substring_index(string, delimiter,-1)

-1 will start at the end of the string.

Solution 4 - Mysql

Combo of reverse/indexof?

SELECT LENGTH(string) - SUBSTRING_INDEX(REVERSE(string), delimiter) + 1

breaking it down, given your Have_a_good_day:

REVERSE('Have_a_good_day') -> yad_doog_a_evaH
SUBSTRING_INDEX('yad_doog_a_evah', '_') -> 4
LENGTH('Have_a_good_day') -> 15
15 - 4 + 1 -> 12

Have_a_good_day
123456789012345
           ^

Solution 5 - Mysql

I found this as a nice trick to do it:

SELECT LOCATE(SUBSTRING_INDEX('Have_a_good_day', '_', -1),'Have_a_good_day')-1 AS indexpos;

This will return the index of the last occurrence (=12). Basically you search for the right part of the string after the last delimiter and then search for the position of this substring in the whole string, which gets you the position :)

If you would like to get the substring to the left of this you can use:

SELECT
  SUBSTRING('Have_a_good_day', 1, 
     LOCATE(SUBSTRING_INDEX('Have_a_good_day', '_', -1),'Have_a_good_day')-1) 
  AS sub;

Solution 6 - Mysql

While the above codes work successfully for a single character, they failed when I used them to find the last occurrence of a substring. I therefore recommend the code below for this task:

SELECT LENGTH("my father is my father")
        - LOCATE('father', REVERSE("my father is my father"))-(LENGTH('father')-1)

This should return 17

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTinyView Question on Stackoverflow
Solution 1 - MysqlcurtView Answer on Stackoverflow
Solution 2 - MysqlN DView Answer on Stackoverflow
Solution 3 - Mysqlrizalp1View Answer on Stackoverflow
Solution 4 - MysqlMarc BView Answer on Stackoverflow
Solution 5 - MysqlAspedView Answer on Stackoverflow
Solution 6 - MysqlAdewole KayodeView Answer on Stackoverflow