- 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
MySQL query to get substrings (only the last three characters) from strings?
For this, you can use SUBSTR() method. 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 the last three characters from every string:
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 a substring from a string except the last three characters?
- MySQL query to get only the minutes from datetime?
- MySQL query to retrieve only the column values with special characters?
- MySQL query to search within the last 5 characters in a column?
- MySQL Query to remove all characters after last comma in string?
- MySQL query to remove everything except the last 7 characters in column record
- MySQL query to get the last created table name (most recent)?
- How to get last 4 characters from string in C#?
- MySQL query to keep only first 2 characters in column value and delete rest of the characters?
- MySQL query to find the average of only first three values from a column with five values
- Count substrings with same first and last characters in C++
- Number of Substrings Containing All Three Characters in C++
- MySQL query to extract last word from a field?
- MySQL query to remove special characters from column values?
- MySQL query to replace special characters from column value

Advertisements