Finding only strings beginning with a number using MySQL Regex?

MySQLMySQLi Database

To find strings beginning with a number, use Regular Expressions. Let us first create a table −

mysql> create table DemoTable
(
   Id varchar(200)
);
Query OK, 0 rows affected (0.59 sec)

Insert records in the table using insert command −

mysql> insert into DemoTable values('123User');
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable values('_$123User');
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable values('User123456');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values('0000User');
Query OK, 1 row affected (0.22 sec)
mysql> insert into DemoTable values('&^*User');
Query OK, 1 row affected (0.24 sec)

Display records from the table using select command −

mysql> select *from DemoTable;

This will produce the following output −

+------------+
| Id         |
+------------+
| 123User    |
| _$123User  |
| User123456 |
| 0000User   |
| &^*User    |
+------------+
5 rows in set (0.00 sec)

Here is the query to find strings starting with a number using MySQL Regexp −

mysql> SELECT DISTINCT Id FROM DemoTable WHERE Id REGEXP '^[0-9]';

This will produce the following output −

+----------+
| Id       |
+----------+
| 123User  |
| 0000User |
+----------+
2 rows in set (0.58 sec)
raja
Published on 29-Apr-2019 08:00:46
Advertisements