MySQL query to select column values ending with certain character/number?


Let us first create a table −

mysql> create table DemoTable
(
   Number int
);
Query OK, 0 rows affected (0.80 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values(189);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values(178);
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable values(876);
Query OK, 1 row affected (0.16 sec)
mysql> insert into DemoTable values(784);
Query OK, 1 row affected (0.23 sec)
mysql> insert into DemoTable values(988);
Query OK, 1 row affected (0.23 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+--------+
| Number |
+--------+
|    189 |
|    178 |
|    876 |
|    784 |
|    988 |
+--------+
5 rows in set (0.00 sec)

Following is the query to select column values ending with a specific character/number. Here, we are focusing on values ending with number 8 −

mysql> select *from DemoTable where Number LIKE '%8';

This will produce the following output −

+--------+
| Number |
+--------+
|    178 |
|    988 |
+--------+
2 rows in set (0.00 sec)

Updated on: 01-Oct-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements