MySQL Regular expressions: How to match digits in the string with \d?

MySQLMySQLi Database

To match digits in the string, the syntax in MySQL is as follows −

select * from yourTab leName where yourColumnName regexp '[0-9]';

To understand the above syntax, let us create a table −

mysql> create table DemoTable1841
     (
     Code varchar(20)
     );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1841 values('John231');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1841 values('19876Sam');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1841 values('Carol');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1841 values('67474');
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1841;

This will produce the following output −

+----------+
| Code     |
+----------+
| John231  |
| 19876Sam |
| Carol    |
| 67474    |
+----------+
4 rows in set (0.00 sec)

Here is the regular expression to match digit in string −

mysql> select * from DemoTable1841 where Code regexp '[0-9]';

This will produce the following output −

+----------+
| Code     |
+----------+
| John231  |
| 19876Sam |
| 67474    |
+----------+
3 rows in set (0.00 sec)
raja
Published on 26-Dec-2019 10:32:15
Advertisements