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('=Candy,2000');
Query OK, 1 row affected (0.53 sec)
mysql> insert into DemoTable1583 values('=Lucky29,10000');
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                                                |
+------------------------------------------------------------+
| =Candy,2000    |
| =Lucky29,10000 |
+------------------------------------------------------------+
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,'=Candy','');
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                                                |
+------------------------------------------------------------+
| ,2000                               |
| =Lucky29,10000 |
+------------------------------------------------------------+
2 rows in set (0.00 sec)
Updated on: 2019-12-16T06:26:55+05:30

322 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements