Which MySQL functions work as similar as LOCATE() function?

MySQLMySQLi Database

MySQL INSTR() and POSITION() function works similar as LOCATE() function. They both are synonyms of LOCATE() function.

INSTR() function also returns the position of the first occurrence of the substring after searching from the string. The syntax of INSTR() is as follows −

Syntax of INSTR()

INSTR(string, substring)

Here, String is the string from which MySQL will search and substring is the string which is to be searched.

Example

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

POSITION() function also returns the position of the first occurrence of the substring after searching from the string. Syntax of POSITION() is as follows −

Syntax of POSITION()

INSTR(substring IN string)

Here, String is the string from which MySQL will search.

Substring is the string which is to be searched.

‘IN’ is a keyword.

Example

mysql> Select POSITION('good' in 'Ram is a good boy')As Result;
+--------+
| Result |
+--------+
|     10 |
+--------+
1 row in set (0.00 sec)
raja
Published on 20-Feb-2018 07:41:00
Advertisements