Deleting partial data from a field in MySQL?


To delete partial data, use UPDATE command along with REPLACE(). Let us first create a table −

mysql> create table DemoTable1583
   -> (
   -> GameDetails text
   -> );
Query OK, 0 rows affected (1.38 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1583 values('<GameName>=Candy</GameName>,<GamePrice>2000</GamePrice>');
Query OK, 1 row affected (0.53 sec)
mysql> insert into DemoTable1583 values('<GameName>=Lucky29</GameName>,<GamePrice>10000</GamePrice>');
Query OK, 1 row affected (0.25 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1583;

This will produce the following output −

+------------------------------------------------------------+
| GameDetails                                                |
+------------------------------------------------------------+
| <GameName>=Candy</GameName>,<GamePrice>2000</GamePrice>    |
| <GameName>=Lucky29</GameName>,<GamePrice>10000</GamePrice> |
+------------------------------------------------------------+
2 rows in set (0.00 sec)

Here is the query to delete partial data from a field in MySQL −

mysql> update DemoTable1583 set GameDetails=replace(GameDetails,'<GameName>=Candy</GameName>','');
Query OK, 1 row affected (0.38 sec)
Rows matched: 2  Changed: 1 Warnings: 0

Let us check the table records once again −

mysql> select * from DemoTable1583;

This will produce the following output −

+------------------------------------------------------------+
| GameDetails                                                |
+------------------------------------------------------------+
| ,<GamePrice>2000</GamePrice>                               |
| <GameName>=Lucky29</GameName>,<GamePrice>10000</GamePrice> |
+------------------------------------------------------------+
2 rows in set (0.00 sec)

Updated on: 16-Dec-2019

178 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements