How can we get some last number of characters from the data stored in a MySQL table’s column?

MySQLMySQLi Database

To get some last number of characters from the data stored in MySQL table’s column, we can use MySQL RIGHT() function. It will return the number of characters specified as it argument. We need to provide the name of the column, having the particular record from which we want to get last characters, as its first argument. To demonstrate it we are taking the example of a table named ‘examination_btech’ having the following examination details of students −

mysql> Select * from examination_btech;
+-----------+----------+--------+
| RollNo    | Name     | Course |
+-----------+----------+--------+
| 201712001 | Rahul    | B.tech |
| 201712002 | Raman    | B.tech |
| 201712003 | Sahil    | B.tech |
| 201712004 | Shalini  | B.tech |
| 201712005 | Pankaj   | B.tech |
| 201712006 | Mohan    | B.tech |
| 201712007 | Yash     | B.tech |
| 201712008 | digvijay | B.tech |
| 201712009 | Gurdas   | B.tech |
| 201712010 | Preeti   | B.tech |
+-----------+----------+--------+
10 rows in set (0.00 sec)

Now if we want to get the last three characters from the series of roll no then it can be done with the help of RIGHT() function as follows −

mysql> Select RIGHT(RollNo, 3) FROM examination_btech;
+------------------+
| RIGHT(RollNo, 3) |
+------------------+
| 001              |
| 002              |
| 003              |
| 004              |
| 005              |
| 006              |
| 007              |
| 008              |
| 009              |
| 010              |
+------------------+
10 rows in set (0.00 sec)
raja
Published on 21-Feb-2018 12:06:21
Advertisements