Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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)
Advertisements
