![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
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
How to TRIM x number of characters, beginning from the last in MySQL?
For this, you can use substring() along with length(). Let us first create a table −
mysql> create table DemoTable1329 -> ( -> StudentName varchar(40) -> ); Query OK, 0 rows affected (0.52 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable1329 values('David Miller'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable1329 values('Chris Brown'); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable1329 values('Adam Smith'); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable1329 values('John Doe'); Query OK, 1 row affected (0.44 sec)
Display all records from the table using select statement −
mysql> select * from DemoTable1329;
This will produce the following output −
+--------------+ | StudentName | +--------------+ | David Miller | | Chris Brown | | Adam Smith | | John Doe | +--------------+ 4 rows in set (0.00 sec)
Here is the query to trim the X number of characters, beginning from the last −
mysql> select substring(StudentName, 1, length(StudentName) - 5) from DemoTable1329;
This will produce the following output −
+----------------------------------------------------+ | substring(StudentName, 1, length(StudentName) - 5) | +----------------------------------------------------+ | David M | | Chris | | Adam | | Joh | +----------------------------------------------------+ 4 rows in set (0.00 sec)
Advertisements