MySQL query to remove everything except the last 7 characters in column record


Let us first create a table −

mysql> create table DemoTable
(
   Strength text
);
Query OK, 0 rows affected (0.51 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('John is a good programmer');
Query OK, 1 row affected (0.09 sec)
mysql> insert into DemoTable values('Chris is a good player in cricket');
Query OK, 1 row affected (0.12 sec)
mysql> insert into DemoTable values('Bob is good in algorithm');
Query OK, 1 row affected (0.42 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable

This will produce the following output −

+-----------------------------------+
| Strength                          |
+-----------------------------------+
| John is a good programmer         |
| Chris is a good player in cricket |
| Bob is good in algorithm          |
+-----------------------------------+
3 rows in set (0.00 sec)

Following is the query to remove everything except the last 7 characters in column record −

mysql> update DemoTable set Strength=substr(Strength,-7,7);
Query OK, 3 rows affected (0.46 sec)
Rows matched: 3 Changed: 3 Warnings: 0

Let us check the table records once again −

mysql> select *from DemoTable;

This will produce the following output −

+----------+
| Strength |
+----------+
| grammer  |
| cricket  |
| gorithm  |
+----------+
3 rows in set (0.00 sec)

Updated on: 26-Sep-2019

71 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements