Tutorialspoint

1 Answer
karthikeya Boyini

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;

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;

This will produce the following output −

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

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.