- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
MySQL query to retrieve only the column values with special characters?
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)
Advertisements