- 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 replace a character in a MySQL table?
To replace only a single character, use REPLACE() in MySQL. Let us first create a table −
mysql> create table DemoTable -> ( -> Name varchar(20) -> ); Query OK, 0 rows affected (0.69 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('John Smitk'); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values('David Miller'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('Adam Smitk'); 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 −
+--------------+ | Name | +--------------+ | John Smitk | | David Miller | | Adam Smitk | +--------------+ 3 rows in set (0.00 sec)
Here is the query to replace a character −
mysql> update DemoTable -> set Name=replace(Name,'k','h'); Query OK, 2 rows affected (0.13 sec) Rows matched: 3 Changed: 2 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+--------------+ | Name | +--------------+ | John Smith | | David Miller | | Adam Smith | +--------------+ 3 rows in set (0.00 sec)
Advertisements