MySQL query to retrieve only the column values with special characters?

MySQLMySQLi Database

For this, use REGEXP. Let us first create a table −

mysql> create table DemoTable(SubjectCode varchar(100));
Query OK, 0 rows affected (0.89 sec)

Insert some records in the table using insert command. The records consists of text, numbers and special characters −

mysql> insert into DemoTable values('Java899@22');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values('C#');
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable values('~Python232');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('MongoDB%');
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable values('C123456');
Query OK, 1 row affected (0.37 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+-------------+
| SubjectCode |
+-------------+
| Java899@22  |
| C#          |
| ~Python232  |
| MongoDB%    |
| C123456    |
+-------------+
5 rows in set (0.00 sec)

Following is the query to get all the values with special characters −

mysql> select *from DemoTable where SubjectCode regexp '[^a-zA-Z0-9\&]';

This will produce the following output −

+-------------+
| SubjectCode |
+-------------+
| Java899@22  |
| C#          |
| ~Python232  |
| MongoDB%    |
+-------------+
4 rows in set (0.00 sec)
raja
Published on 22-Aug-2019 08:20:11
Advertisements