How to perform string matching in MySQL?

MySQLMySQLi Database

For string matching, use LIKE operator. Let us first create a table −

mysql> create table DemoTable
   -> (
   -> MonthName varchar(100)
   -> );
Query OK, 0 rows affected (0.63 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('JFMA');
Query OK, 1 row affected (0.19 sec)

mysql> insert into DemoTable values('JMA');
Query OK, 1 row affected (0.16 sec)

mysql> insert into DemoTable values('JDN');
Query OK, 1 row affected (0.16 sec)

mysql> insert into DemoTable values('JFOSA');
Query OK, 1 row affected (0.19 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

+-----------+
| MonthName |
+-----------+
| JFMA      |
| JMA       |
| JDN       |
| JFOSA     |
+-----------+
4 rows in set (0.00 sec)

Following is the query to perform string matching in MySQL −

mysql> select *from DemoTable WHERE MonthName LIKE '%J%M%';

Output

+-----------+
| MonthName |
+-----------+
| JFMA      |
| JMA       |
+-----------+
2 rows in set (0.00 sec)
raja
Published on 09-Jul-2019 15:25:27
Advertisements