- Trending Categories
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
How to escape '%' character on the left and middle part of strings in a MySQL column?
Let us first create a table −
mysql> create table DemoTable629 (StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,StudentSubject text); Query OK, 0 rows affected (0.77 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable629(StudentSubject) values('MySQL%'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable629(StudentSubject) values('Spring%Hibernate'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable629(StudentSubject) values('%Java'); Query OK, 1 row affected (0.21 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable629;
This will produce the following output −
+-----------+------------------+ | StudentId | StudentSubject | +-----------+------------------+ | 1 | MySQL% | | 2 | Spring%Hibernate | | 3 | %Java | +-----------+------------------+ 3 rows in set (0.00 sec)
Following is the query to escape '%' character −
mysql> select *from DemoTable629 where StudentSubject like '%\%';
This will produce the following output −
+-----------+----------------+ | StudentId | StudentSubject | +-----------+----------------+ | 1 | MySQL% | +-----------+----------------+ 1 row in set (0.00 sec)
Advertisements