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)
Updated on: 2019-08-23T07:48:55+05:30

151 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements