How Are MySQL INSTR() and LIKE operator similar?

MySQLMySQLi Database

We can use both INSTR() function and LIKE operator to search or match a particular pattern and they return same result. It can be demonstrated from the following example of ‘Student’ table.

Example

Suppose we want to search name, which contains ‘av’ in it, from ‘Student’ table. We can use INSTR() function as follows −

mysql> Select Name from student where INSTR(name, 'av') > 0;
+--------+
| Name   |
+--------+
| Gaurav |
| Aarav  |
| Gaurav |
+--------+
3 rows in set (0.00 sec)

Now, for the same kind of search we can use LIKE operator as follows −

mysql> Select Name from student where Name LIKE '%av%';
+--------+
| Name   |
+--------+
| Gaurav |
| Aarav  |
| Gaurav |
+--------+
3 rows in set (0.00 sec)

Both the queries above return the same result.

raja
Published on 20-Feb-2018 11:05:25
Advertisements