Get only digits using regexp in MySQL?

If you want to get only digits using REGEXP, use the following regular expression( ^[0-9]*$) in where clause.
Case 1 − If you want only those rows which have exactly 10 digits and all must be only digit, use the below regular expression.
SELECT *FROM yourTableName
WHERE yourColumnName REGEXP '^[0-9]{10}$';
Case 2 − If you want only those rows with the digit either 1 or more, the following is the syntax −
SELECT *FROM yourTableName
WHERE yourColumnName REGEXP '^[0-9]*$';
The above syntax will give only those rows that do not have any any characters.
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> insert into OnlyDigits(UserId, UserName) values('123User1','John');
Query OK, 1 row affected (0.16 sec)
mysql> insert into OnlyDigits(UserId, UserName) values('3445User2','Carol');
Query OK, 1 row affected (0.19 sec)
mysql> insert into OnlyDigits(UserId, UserName) values('145363User3','Mike');
Query OK, 1 row affected (0.20 sec)
mysql> insert into OnlyDigits(UserId, UserName) values('13455User4','Larry');
Query OK, 1 row affected (0.22 sec)
mysql> insert into OnlyDigits(UserId, UserName) values('123555User5','Sam');
Query OK, 1 row affected (0.15 sec)
mysql> insert into OnlyDigits(UserId, UserName) values('596766User6','David');
Query OK, 1 row affected (0.24 sec)
mysql> insert into OnlyDigits(UserId, UserName) values('96977User7','Robert');
Query OK, 1 row affected (0.15 sec)
mysql> insert into OnlyDigits(UserId, UserName) values('99999User8','James');
Query OK, 1 row affected (0.20 sec)
mysql> insert into OnlyDigits(UserId, UserName) values('9999999','James');
Query OK, 1 row affected (0.13 sec)
mysql> insert into OnlyDigits(UserId, UserName) values('153545','Maxwell');
Query OK, 1 row affected (0.23 sec)
mysql> insert into OnlyDigits(UserId, UserName) values('9999986787','Johnson');
Query OK, 1 row affected (0.20 sec)
Now you can display all records from the table using select statement. The query is as follows −
mysql> select *from OnlyDigits;
The following is the output −
+----+-------------+----------+
| Id | UserId      | UserName |
+----+-------------+----------+
|  1 | 123User1    | John     |
|  2 | 3445User2   | Carol    |
|  3 | 145363User3 | Mike     |
|  4 | 13455User4  | Larry    |
|  5 | 123555User5 | Sam      |
|  6 | 596766User6 | David    |
|  7 | 96977User7  | Robert   |
|  8 | 99999User8  | James    |
|  9 | 9999999     | James    |
| 10 | 153545      | Maxwell |
| 11 | 9999986787  | Johnson |
+----+-------------+----------+
11 rows in set (0.00 sec)
Case 1 − The following is the query with regex when you want exactly 10 digits in a string and all must be a number:
mysql> select *from OnlyDigits
-> where UserId REGEXP '^[0-9]{10}$';
The following is the output −
+----+------------+----------+
| Id | UserId     | UserName |
+----+------------+----------+
| 11 | 9999986787 | Johnson  |
+----+------------+----------+
1 row in set (0.00 sec)
Alternate regular expression of case 1. The query is as follows:
mysql> select *from OnlyDigits
-> where UserId REGEXP '^[[:digit:]]{10}$';
The following is the output −
+----+------------+----------+
| Id | UserId     | UserName |
+----+------------+----------+
| 11 | 9999986787 | Johnson |
+----+------------+----------+
1 row in set (0.00 sec)
Case 2 − If you want only those rows with the one or more digit (Only the digits). The query is as follows:
mysql> select *from OnlyDigits
-> where UserId REGEXP'^[0-9]*$';
The following is the output:
+----+------------+----------+
| Id | UserId     | UserName |
+----+------------+----------+
| 9  | 9999999    | James    |
| 10 | 153545     | Maxwell  |
| 11 | 9999986787 | Johnson  |
+----+------------+----------+
3 rows in set (0.00 sec)