How to select everything before @ in an email-id with in MySQL?


Use SUBSTRING_INDEX to select everything before @ in an email-id −

select substring_index(yourColumnName,'@',1) from yourTableName;

Let us first create a table −

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

Insert some records in the table using insert command −

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

mysql> insert into DemoTable values('987Sam@hotmail.com');
Query OK, 1 row affected (0.13 sec)

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

Display all records from the table using select statement −

mysql> select *from DemoTable;

Output

This will produce the following output −

+--------------------------+
| EmployeeMailId           |
+--------------------------+
| Larry123@gmail.com       |
| 987Sam@hotmail.com       |
| 123456David_98@gmail.com |
+--------------------------+
3 rows in set (0.00 sec)

Here is the query to select everything before @ in an Email-id −

mysql> select substring_index(EmployeeMailId,'@',1) from DemoTable;

Output

This will produce the following output −

+---------------------------------------+
| substring_index(EmployeeMailId,'@',1) |
+---------------------------------------+
| Larry123                              |
| 987Sam                                |
| 123456David_98                        | 
+---------------------------------------+
3 rows in set (0.00 sec)

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jun-2020

530 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements