Apostrophe replacement in MySQL query?


To replace apostrophe, you can use replace(). Following is the syntax −

update yourTableName set yourColumnName=replace(yourColumnName,'\'','');

Let us first create a table −

mysql> create table DemoTable
   (
   Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   Sentence varchar(100)
   );
Query OK, 0 rows affected (0.17 sec)

Insert some records in the table using insert command. Apostrophe is added for the sentence −

mysql> insert into DemoTable(Sentence) values('Chris\'s Father');
Query OK, 1 row affected (0.07 sec)

mysql> insert into DemoTable(Sentence) values('Larry\'s Mother');
Query OK, 1 row affected (0.06 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----+----------------+
| Id | Sentence       |
+----+----------------+
| 1  | Chris's Father |
| 2  | Larry's Mother |
+----+----------------+
2 rows in set (0.00 sec)

Following is the query to replace apostrophe −

mysql> update DemoTable set Sentence=replace(Sentence,'\'','');
Query OK, 2 rows affected (0.07 sec)
Rows matched: 2 Changed: 2 Warnings: 0

Let us display records from the table to check apostrophe have been replaced or not.

mysql> select *from DemoTable;

This will produce the following output −

+----+---------------+
| Id | Sentence      |
+----+---------------+
| 1  | Chriss Father |
| 2  | Larrys Mother |
+----+---------------+
2 rows in set (0.00 sec)

Updated on: 30-Jul-2019

431 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements