- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove trailing numbers surrounded by parenthesis from a MySQL column
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)
Advertisements