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('[email protected]'); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable values('[email protected]'); 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 | +----------------------+ | [email protected] | | [email protected] | +----------------------+ 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)