Update a column in MySQL and remove the trailing underscore values

MySQLMySQLi Database

To remove the trailing values, use TRIM() as in the below update syntax −

update yourTableName
set yourColumnName=trim(trailing '_' from yourColumnName);

Let us first create a table −

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

Insert some records in the table using insert command −

mysql> insert into DemoTable1521 values('345_');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable1521 values('12345');
Query OK, 1 row affected (0.38 sec)
mysql> insert into DemoTable1521 values('9084_');
Query OK, 1 row affected (1.29 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1521;

This will produce the following output −

+-------------+
| StudentCode |
+-------------+
| 345_        |
| 12345       |
| 9084_       |
+-------------+
3 rows in set (0.00 sec)

Here is the query to update column in MySQL and trim −

mysql> update DemoTable1521
   -> set StudentCode=trim(trailing '_' from StudentCode);
Query OK, 2 rows affected (0.34 sec)
Rows matched: 3  Changed: 2 Warnings: 0

Let us check the table records once again −

mysql> select * from DemoTable1521;

This will produce the following output −

+-------------+
| StudentCode |
+-------------+
| 345         |
| 12345       |
| 9084        |
+-------------+
3 rows in set (0.00 sec)
raja
Published on 11-Dec-2019 06:25:54
Advertisements