MySQL query to replace special characters from column value



Let us first create a table −

mysql> create table DemoTable1574    -> (    -> StudentCode varchar(20)    -> ); Query OK, 0 rows affected (0.59 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1574 values('111_Carol'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1574 values('______'); Query OK, 1 row affected (0.30 sec) mysql> insert into DemoTable1574 values('David_12345'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable1574 values('______'); Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1574;

This will produce the following output −

+-------------+ | StudentCode | +-------------+ | 111_Carol   | | ______      | | David_12345 | | ______      | +-------------+ 4 rows in set (0.00 sec)

Here is the query to replace −

mysql> update DemoTable1574 set StudentCode=replace(StudentCode,'______','Mike_007'); Query OK, 2 rows affected (0.38 sec) Rows matched: 4  Changed: 2 Warnings: 0

Let us check the table records once again −

mysql> select * from DemoTable1574;

This will produce the following output −

+-------------+ | StudentCode | +-------------+ | 111_Carol   | | Mike_007    | | David_12345 | | Mike_007    | +-------------+ 4 rows in set (0.00 sec)
Updated on: 2019-12-16T06:08:49+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements