How to escape parentheses in MySQL REGEXP clause and display only specific values with parentheses?


Let us first create a table −

mysql> create table DemoTable1908
   (
   Code text
   );
Query OK, 0 rows affected (0.00 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1908 values('MySQL(1)Database');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1908 values('MongoDB 2 Database');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1908 values('MySQL(3)Database');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1908 values('SQL Server(10)Database');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1908 values('MySQL 8 Database');
Query OK, 1 row affected (0.00 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1908;

This will produce the following output −

+------------------------+
| Code                   |
+------------------------+
| MySQL(1)Database       |
| MongoDB 2 Database     |
| MySQL(3)Database       |
| SQL Server(10)Database |
| MySQL 8 Database       |
+------------------------+
5 rows in set (0.00 sec)

Here is the query to escape parentheses in a REGEXP clause and display only the paratheses value with () −

mysql> select * from DemoTable1908 where Code regexp '^MySQL[(][0-9][)]Database';

This will produce the following output −

+------------------+
| Code             |
+------------------+
| MySQL(1)Database |
| MySQL(3)Database |
+------------------+
2 rows in set (0.00 sec)

Updated on: 30-Dec-2019

584 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements