MySQL query not matching due to punctuation?


Use the MySQL LIKE operator to match even when punctuation is present. Let us first create a table−

mysql> create table DemoTable
   -> (
   -> Comments varchar(20)
   -> );
Query OK, 0 rows affected (1.10 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Good,Morning');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('Nice');
Query OK, 1 row affected (0.51 sec)
mysql> insert into DemoTable values('good,bye!');
Query OK, 1 row affected (0.11 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------------+
|  Comments    |
+--------------+
| Good,Morning |
| Nice         |
| good,bye!    |
+--------------+
3 rows in set (0.00 sec)

Following is the query to match even when punctuation is present −

mysql> select *from DemoTable where Comments like '%good%';

This will produce the following output −

+--------------+
| Comments     |
+--------------+
| Good,Morning |
| good,bye!    |
+--------------+
2 rows in set (0.00 sec)

Updated on: 12-Dec-2019

79 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements