Finding only strings beginning with a number using MySQL Regex?


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)

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

159 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements