Tutorialspoint

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


1 Answer
Rama Giri

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;

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%';

This will produce the following output −

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

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.