
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Select Domain Name from Email Address in MySQL
To select domain name from email address, you can use in-built SUBSTRING_INDEX() function from MySQL.
To understand the concept, let us create a table. The following is the query to create a table.
mysql> create table selectDomainNameOnly −> ( −> UserEmailAddress varchar(200) −> ); Query OK, 0 rows affected (0.52 sec)
Insert records in the table using insert command. The record will have email-ids from which we need to fetch the domain name. The query is as follows −
mysql> insert into selectDomainNameOnly values('[email protected]'); Query OK, 1 row affected (0.10 sec) mysql> insert into selectDomainNameOnly values('[email protected]'); Query OK, 1 row affected (0.24 sec) mysql> insert into selectDomainNameOnly values('[email protected]'); Query OK, 1 row affected (0.16 sec)
Display all records from table using select statement. The query is as follows −
mysql> select *from selectDomainNameOnly;
The following is the output −
+------------------------+ | UserEmailAddress | +------------------------+ | [email protected] | | [email protected] | | [email protected] | +------------------------+ 3 rows in set (0.00 sec)
The following is the query to select only domain name. The query is as follows −
mysql> select (SUBSTRING_INDEX(SUBSTR(UserEmailAddress, INSTR(UserEmailAddress, '@') + 1),'.',1)) as DomainName from selectDomainNameOnly;
The following is the output −
+------------+ | DomainName | +------------+ | yahoo | | gmail | | hotmail | +------------+ 3 rows in set (0.06 sec)
Advertisements