Find records with a specific last digit in column with MySQL

MySQLMySQLi Database

For this, use the RIGHT() method to fetch records with a specific last digit. Let us first create a table −

mysql> create table DemoTable823(Value varchar(100));
Query OK, 0 rows affected (0.54 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable823 values('9847826');
Query OK, 1 row affected (0.52 sec)
mysql> insert into DemoTable823 values('84747464');
Query OK, 1 row affected (0.13 sec)
mysql> insert into DemoTable823 values('9899889883');
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable823 values('123456');
Query OK, 1 row affected (0.10 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable823;

This will produce the following output −

+------------+
| Value      |
+------------+
| 9847826    |
| 84747464   |
| 9899889883 |
| 123456     |
+------------+
4 rows in set (0.00 sec)

Following is the query to find records with a specific last digit −

mysql> select *from DemoTable823 where right(Value,1)='6';

This will produce the following output −

+---------+
| Value   |
+---------+
| 9847826 |
| 123456  |
+---------+
2 rows in set (0.00 sec)
raja
Published on 03-Sep-2019 11:56:39
Advertisements