What would be the simplest way to locate the index of the third space in a string.
My goal is to get CCC out of this space separated list: AAAA BBBB CCCC DDDD EEE. where A and B and D are fixed length, and C is variable length, E F G are optional.
In Java I would use indexof, with a starting point of 10 and that would get me the third space, but it seems that I cannot do that in MySQL, so I thought maybe I could find a 'third index of' function?
You would want to use SUBSTRING_INDEX function like this
SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(field, ' ', 3), ' ', -1)
FROM table
The inner function call would get you to AAAA BBBB CCCC while the outer function call would pare that down to just CCCC.
You could use SUBSTRING_INDEX.
Generally you can select the nth word in a string using:
SET @N = 3; -- 3rd word
SET @delimiter = ' ';
SELECT
SUBSTRING_INDEX(SUBSTRING_INDEX(words, @delimiter, @N), @delimiter, -1)
FROM
my_table
来源:https://stackoverflow.com/questions/14347581/mysql-second-or-third-index-of-in-string