What happens if the substring is there for more than one time in the string given as the arguments of LOCATE() function?


In case if the substring is there for more than one time in the string then MySQL LOCATE() function will return the position of the first occurrence of the substring.

Example

mysql> Select LOCATE('good','Ram is a good boy. Is Ram a good boy?')As Result;
+--------+
| Result |
+--------+
|     10 |
+--------+
1 row in set (0.00 sec)

As we can see that the substring ‘good’ is in the string for two times. The first occurrence is at position 10 and another occurrence is at position 29. MySQL returns the position of the first occurrence.

Updated on: 03-Feb-2020

47 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements