Find position of word in string

user1251973 :

I have string

good morning what a lovely day today

I need to find the position of 'lovely'. Query should return the result 5.

GMB :

You can use find_in_set() for this:

select find_in_set('lovely', replace('good morning what a lovely day today', ' ', ','))

Yields:

 5

From the documentation:

FIND_IN_SET(str,strlist)

Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by , characters.

So basically the logic is to replace space characters with commas, and then use that function.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=195311&siteId=1