MySQL query to search within the last 5 characters in a column?


Let us first create a table −

mysql> create table DemoTable
   -> (
   -> EmployeeName varchar(100)
   -> );
Query OK, 0 rows affected (0.81 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('Adam Smith');
Query OK, 1 row affected (0.12 sec)

mysql> insert into DemoTable values('Carol Taylor');
Query OK, 1 row affected (0.12 sec)

mysql> insert into DemoTable values('David Miller');
Query OK, 1 row affected (0.20 sec)

mysql> insert into DemoTable values('Chris Evan');
Query OK, 1 row affected (0.13 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+--------------+
| EmployeeName |
+--------------+
| Adam Smith   |
| Carol Taylor |
| David Miller |
| Chris Evan   |
+--------------+
4 rows in set (0.00 sec)

Following is the query to search within the last 5 characters of a column −

mysql> select *from DemoTable where Right(EmployeeName, InStr(EmployeeName, ' ' )) LIKE 'Mille%';

Output

This will produce the following output −

+--------------+
| EmployeeName |
+--------------+
| David Miller |
+--------------+
1 row in set (0.00 sec)

Updated on: 30-Jun-2020

138 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements