- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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)
- Related Articles
- How can we get some last number of characters from the data stored in a MySQL table’s column?
- How to trim down non printable characters from a string in Python?
- How to get the maximum value from a column with alphanumeric strings beginning with specific characters in MYSQL?
- How to extract the last 4 characters from NSString?
- MySQL query to get substrings (only the last three characters) from strings?
- How to change auto increment number in the beginning in MySQL?
- How to get last 4 characters from string in C#?
- MySQL query to get a substring from a string except the last three characters?
- Discard last 3 characters of a field in MySQL
- Strip last two characters of a column in MySQL?
- How to trim commas with MySQL?
- How to extract the last n characters from a string using Java?
- How to get last 2 characters from string in C# using Regex?
- MySQL query to search within the last 5 characters in a column?
- How do you select from MySQL where last value in a string = x?

Advertisements