Using Regex find strings containing a-z, A-Z and 0-9 in MySQL

MySQLMySQLi Database

To find strings containing a-z, A-Z and 0-9, use BINARY REGEXP along with AND operator.

Let us first create a table −

mysql> create table DemoTable738 (UserId varchar(100));
Query OK, 0 rows affected (0.81 sec)

Insert some records in the table using insert command  −

mysql> insert into DemoTable738 values('John');
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable738 values('sAm456');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable738 values('98Carol');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable738 values('67david');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable738 values('69MIKE');
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable738;

This will produce the following output -

+---------+
| UserId  |
+---------+
| John    |
| sAm456  |
| 98Carol |
| 67david |
| 69MIKE  |
+---------+
5 rows in set (0.00 sec)

Following is the query to find strings using REGEXP−

mysql> select *from DemoTable738 where UserId REGEXP BINARY '[A-Z]' 
   and UserId REGEXP BINARY '[a-z]' and UserId REGEXP BINARY '[0-9]';

This will produce the following output -

+---------+
| UserId  |
+---------+
| sAm456  |
| 98Carol |
+---------+
2 rows in set (0.03 sec)
raja
Published on 22-Aug-2019 08:00:19
Advertisements