Search for specific characters within a string with MySQL?


For this, use REGEXP. For example, characters J, A, V and A. Let us first create a table −

mysql> create table DemoTable
(
   Value varchar(50)
);
Query OK, 0 rows affected (3.92 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('XYSJGHAKLMVDFFSA');
Query OK, 1 row affected (0.67 sec)
mysql> insert into DemoTable values('PQRSTJAVAL');
Query OK, 1 row affected (0.58 sec)
mysql> insert into DemoTable values('SJATUVAK');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('JSTUVA');
Query OK, 1 row affected (0.32 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+------------------+
| Value            |
+------------------+
| XYSJGHAKLMVDFFSA |
| PQRSTJAVAL       |
| SJATUVAK         |
| JSTUVA           |
+------------------+
4 rows in set (0.00 sec)

Following is the query to search for characters within a string with MySQL −

mysql> select *from DemoTable where Value REGEXP '.*J.*A.*V.*A';

This will produce the following output −

+------------------+
| Value            |
+------------------+
| XYSJGHAKLMVDFFSA |
| PQRSTJAVAL       |
| SJATUVAK         |
+------------------+
3 rows in set (0.00 sec)
raja
Published on 09-Oct-2019 15:39:26
Advertisements