For this, use trim() along with substring(). Let us first create a table −
mysql> create table DemoTable ( Name varchar(100) ); Query OK, 0 rows affected (0.80 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('1stJohn'); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable values('1stJohn (7)'); Query OK, 1 row affected (0.65 sec) mysql> insert into DemoTable values('2ndSam'); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable values('2ndSam (4)'); Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------------+ | Name | +-------------+ | 1stJohn | | 1stJohn (7) | | 2ndSam | | 2ndSam (4) | +-------------+ 4 rows in set (0.00 sec)
Following is the query to remove trailing numbers surrounded by parenthesis from a MySQL column −
mysql> select trim(substring(Name, 1, (CHAR_LENGTH(Name) - LOCATE('(', REVERSE(Name))))) AS RemovingTrailingNumbers from DemoTable;
This will produce the following output −
+-------------------------+ | RemovingTrailingNumbers | +-------------------------+ | 1stJohn | | 1stJohn | | 2ndSam | | 2ndSam | +-------------------------+ 4 rows in set (0.00 sec)