How to get last 12 digits from a string in MySQL?

MySQLMySQLi Database

You can use RIGHT() function from MySQL to get the last 12 digits from a string. Let us first create a table −

mysql> create table DemoTable
   (
   Number varchar(200)
   );
Query OK, 0 rows affected (0.59 sec)

Insert records in the table using insert command −

mysql> insert into DemoTable values('7437647847847474374747464647484949959958484');
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values('9990000399494959697080800007007070808080808');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('1211111212344554444443333345555554433333333333333');
Query OK, 1 row affected (0.14 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable;

This will produce the following output −

+---------------------------------------------------+
| Number                                            |
+---------------------------------------------------+
| 7437647847847474374747464647484949959958484       |
| 9990000399494959697080800007007070808080808       |
| 1211111212344554444443333345555554433333333333333 |
+---------------------------------------------------+
3 rows in set (0.00 sec)

Following is the query to get last 12 digits from a string in MySQL −

mysql> select right(Number,12) as `Last 12 Digit From String` from DemoTable;

This will produce the following output −

+---------------------------+
| Last 12 Digit From String |
+---------------------------+
| 949959958484              |
| 070808080808              |
| 333333333333              |
+---------------------------+
3 rows in set (0.00 sec)
raja
Published on 03-May-2019 12:05:16
Advertisements