- Trending Categories
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
MySQL query to get a substring from a string except the last three characters?
For this, you can use SUBSTR along with length().
Let us first create a table −
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, FirstName varchar(20) ); Query OK, 0 rows affected (1.31 sec)
Following is the query to insert some records in the table using insert command −
mysql> insert into DemoTable(FirstName) values('John'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(FirstName) values('Carol'); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable(FirstName) values('Robert'); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(FirstName) values('Chris'); Query OK, 1 row affected (0.16 sec) mysql> insert into DemoTable(FirstName) values('David'); Query OK, 1 row affected (0.17 sec)
Following is the query to display records from the table using select command −
mysql> select *from DemoTable;
This will produce the following output −
+----+-----------+ | Id | FirstName | +----+-----------+ | 1 | John | | 2 | Carol | | 3 | Robert | | 4 | Chris | | 5 | David | +----+-----------+ 5 rows in set (0.00 sec)
Following is the query to get a substring removing the last 3 characters −
mysql> select substr(FirstName,1,length(FirstName)-3) from DemoTable;
This will produce the following output −
+-----------------------------------------+ | substr(FirstName,1,length(FirstName)-3) | +-----------------------------------------+ | J | | Ca | | Rob | | Ch | | Da | +-----------------------------------------+ 5 rows in set (0.00 sec)
To get same output, you can use following alternate query −
mysql> select left(FirstName,length(FirstName)-3) from DemoTable;
This will produce the following output −
+-------------------------------------+ | left(FirstName,length(FirstName)-3) | +-------------------------------------+ | J | | Ca | | Rob | | Ch | | Da | +-------------------------------------+ 5 rows in set (0.00 sec)
To get last three characters, you can use the following query −
mysql> select substr(FirstName,-3) from DemoTable;
This will produce the following output −
+----------------------+ | substr(FirstName,-3) | +----------------------+ | ohn | | rol | | ert | | ris | | vid | +----------------------+ 5 rows in set (0.00 sec)
- Related Articles
- MySQL query to get substrings (only the last three characters) from strings?
- MySQL query to remove everything except the last 7 characters in column record
- Get the index of last substring in a given string in MySQL?
- MySQL Query to remove all characters after last comma in string?
- How to get last 4 characters from string in\nC#?
- MySQL query to search within the last 5 characters in a column?
- Golang Program to get a substring from the string
- How to get last 12 digits from a string in MySQL?
- Get the last occurrence of a substring within a string in Arduino
- MySQL query to display a substring before a special character in a string
- MySQL query to select a specific string with special characters
- How to get last 2 characters from string in C# using Regex?
- MySQL query to extract last word from a field?
- How to select all rows from a table except the last one in MySQL?
- How to extract the last n characters from a string using Java?

Advertisements