MySQL query to select all fields beginning with a given number with next character a letter?

MySQLMySQLi Database

You can use REGEXP for this. Let us first create a table −

mysql> create table DemoTable
   (
   Value text
   );
Query OK, 0 rows affected (1.28 sec)

Insert records in the table using insert command −

mysql> insert into DemoTable values('645st');
Query OK, 1 row affected (0.25 sec)
mysql> insert into DemoTable values('765stp');
Query OK, 1 row affected (0.14 sec)
mysql> insert into DemoTable values('665tcp');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values('606cpp');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable;

This will produce the following output −

+--------+
| Value  |
+--------+
| 645st  |
| 765stp |
| 665tcp |
| 606cpp |
+--------+
4 rows in set (0.00 sec)

Following is the query to select all fields beginning with a given number with the next character a letter −

mysql> select *from DemoTable
WHERE Value REGEXP '^6[[:alnum:]]';

This will produce the following output −

+--------+
| Value  |
+--------+
| 645st  |
| 665tcp |
| 606cpp |
+--------+
3 rows in set (0.01 sec)
raja
Published on 03-May-2019 08:45:03
Advertisements