MySQL query to fecth the domain name from email-id?


Use SUBSTRING_INDEX() for this. Let us first create a table −

mysql> create table DemoTable
-> (
-> UserMailId varchar(100)
-> );
Query OK, 0 rows affected (0.68 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable values('John@gmail.com');
Query OK, 1 row affected (0.20 sec)

mysql> insert into DemoTable values('Carol94844@yahoo.com');
Query OK, 1 row affected (0.25 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+----------------------+
| UserMailId           |
+----------------------+
| John@gmail.com       |
| Carol94844@yahoo.com |
+----------------------+
2 rows in set (0.00 sec)

Following is the query to fetch the domain name −

mysql> select substring_index(UserMailId,'@',-1) as RightSideValue from DemoTable;

Output

This will produce the following output −

+----------------+
| RightSideValue |
+----------------+
| gmail.com      |
| yahoo.com      |
+----------------+
2 rows in set (0.00 sec)

Updated on: 30-Jun-2020

517 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements