- 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
Strip last two characters of a column in MySQL?
You can strip last two characters with the help of SUBSTRING() and CHAR_LENGTH() methods. The syntax is as follows −
select yourColumnName,SUBSTRING(yourColumnName,1,CHAR_LENGTH(yourColumnName) - 2) AS anyVariableName from yourTableName;
To understand the above syntax, let us create a table −
mysql> create table LastTwoCharacters −> ( −> Words varchar(200) −> ); Query OK, 0 rows affected (0.71 sec)
Now you can insert some records in the table with the help of select statement. The query to insert records is as follows −
mysql> insert into LastTwoCharacters values('Hellooo'); Query OK, 1 row affected (0.23 sec) mysql> insert into LastTwoCharacters values('Worldsss'); Query OK, 1 row affected (0.10 sec) mysql> insert into LastTwoCharacters values('Johnson'); Query OK, 1 row affected (0.22 sec)
Display all records with the help of select statement −
mysql> select *from LastTwoCharacters;
The following is the output −
+----------+ | Words | +----------+ | Hellooo | | Worldsss | | Johnson | +----------+ 3 rows in set (0.00 sec)
The following is the query to strip the last two characters of a column −
mysql> select Words,SUBSTRING(Words,1,CHAR_LENGTH(Words) - 2) AS AfterStripLastTwoChar from LastTwoCharacters;
The following is the output −
+----------+-----------------------+ | Words | AfterStripLastTwoChar | +----------+-----------------------+ | Hellooo | Hello | | Worldsss | Worlds | | Johnson | Johns | +----------+-----------------------+ 3 rows in set (0.00 sec)
- Related Articles
- MySQL query to search within the last 5 characters in a column?
- Discard last 3 characters of a field in MySQL
- How to strip all spaces from a column in MySQL?
- MySQL query to remove everything except the last 7 characters in column record
- How can we get some last number of characters from the data stored in a MySQL table’s column?
- MySQL query to delete last two words from every column value
- Append special characters to column values in MySQL
- MySQL query to split a column after specific characters?
- How to TRIM x number of characters, beginning from the last in MySQL?
- Find records with a specific last digit in column with MySQL
- Remove first two characters of all fields in MySQL?
- MySQL Query to remove all characters after last comma in string?
- Java Program to strip a filename of its extension after the last dot
- How to select all the characters after the first 20 characters from a column in MySQL?
- How to get first N characters from a MySQL column?

Advertisements