Fetch the substring after last dot in MySQL

MySQLMySQLi Database

To fetch the substring after last dot, use substring_index(). Let us first create a table −

mysql> create table DemoTable1341
   -> (
   -> Value varchar(60)
   -> );
Query OK, 0 rows affected (0.75 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1341 values('John.123.@gmail.com' );
Query OK, 1 row affected (0.27 sec)
mysql> insert into DemoTable1341 values('Carol.Taylor.gmail') ;
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable1341 values('C.MyFolder.Location') ;
Query OK, 1 row affected (0.10 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1341;

This will produce the following output −

+---------------------+
| Value               |
+---------------------+
| John.123.@gmail.com |
| Carol.Taylor.gmail  |
| C.MyFolder.Location |
+---------------------+
3 rows in set (0.00 sec)

Here is the query to fetch the substring after last dot −

mysql> select substring_index(Value, '.', -1) from DemoTable1341;

This will produce the following output −

+---------------------------------+
| substring_index(Value, '.', -1) |
+---------------------------------+
| com                             |
| gmail                           |
| Location                        |
+---------------------------------+
3 rows in set (0.00 sec)
raja
Published on 05-Nov-2019 07:23:14
Advertisements