Extract domain of Email from table in SQL Server
Last Updated :
07 Dec, 2020
Introduction :
As a DBA, you might come across a request where you need to extract the domain of the email address, the email address that is stored in the database table. In case you want to count the most used domain names from email addresses in any given table, you can count the number of extracted domains from Email in SQL Server as shown below.
SQL Queries could be used to extract the domain from the Email address.
Let us created table named "
email_demo" -
create table (ID int, Email varchar (200));
Inserting values in the table email_demo -
insert into email_demo values(
(1, '[email protected]'), (2, '[email protected]'),
(3, '[email protected]'), (4, '[email protected]'),
(5, '[email protected]'), (6, '[email protected]'),
(7, '[email protected]'), (8, '[email protected]'),
(9, '[email protected]'), (10, '[email protected]'),
(11, '[email protected]'), (12, '[email protected]'),
(13, '[email protected]'), (14, '[email protected]'),
(15, '[email protected]'), (16, '[email protected]'),
(17, '[email protected]'));
Displaying the table contents -
select * from email_demo;
1. Extract Domain From Email in SQL Server :
In below example we will use
SUBSTRING function to select the string after the @ symbol in each of the value.
SQL Extract Domain From Email -
SELECT ID,
SUBSTRING ([Email], CHARINDEX( '@', [Email]) + 1,
LEN([Email])) AS [Domain]
FROM [email_demo];
OUTPUT :
ID |
Domain |
1 |
gfg.com |
2 |
gfg.com |
3 |
gfg.org |
4 |
xyz.com |
5 |
xyz.com |
6 |
xyz.com |
7 |
gfg.com |
8 |
gfg.com |
9 |
abc.com |
10 |
mno.com |
11 |
gfg.com |
12 |
abc.com |
13 |
mno.com |
14 |
abc.com |
15 |
xyz.com |
16 |
gfg.org |
17 |
abc.com |
Approached used :
- Here we assigned the Source as our Column Name 'Email' in the SUBSTRING function.
- Next, we used the CHARINDEX function to find the @ symbol, and then added 1, so that the starting point will be after the @ symbol.
- Then, we used the LEN function to specify the end value.
2. Count the number of extracted Domain From Email in SQL Server :
Approach 1 : SQL Query to Count Number of Records for Extract Domain name From Email -
SELECT RIGHT ([Email],
LEN([Email]) - CHARINDEX( '@', [Email])) AS [Domain],
COUNT(*) AS [Total Number of Domain]
FROM [email_demo]
WHERE LEN([Email ]) > 0
GROUP BY RIGHT([Email],
LEN([Email]) - CHARINDEX( '@', [Email]));
OUTPUT :
Domain |
Total Number of Domain |
abc.com |
4 |
gfg.com |
5 |
gfg.org |
2 |
mno.com |
2 |
xyz.com |
4 |
Approach 2 : SQL Query to Count Number of Records for Extract Domain name From Email -
SELECT SUBSTRING ([Email],
CHARINDEX( '@', [Email] ) + 1, LEN([Email])) AS [Domain],
COUNT(*) AS [Total Number of Domain]
FROM [email_demo]
WHERE LEN([Email]) > 1
GROUP BY SUBSTRING ([Email], CHARINDEX( '@', [Email] ) + 1,
LEN([Email]));
OUTPUT :
Domain |
Total Number of Domain |
abc.com |
4 |
gfg.com |
5 |
gfg.org |
2 |
mno.com |
2 |
xyz.com |
4 |
Similar Reads
Table operations in MS SQL Server
In a relational database, the data is stored in the form of tables and each table is referred to as a relation. A table can store a maximum of 1000 rows. Tables are a preferred choice as: Tables are arranged in an organized manner. We can segregate the data according to our preferences in the form o
2 min read
Export SQL Server Data From Table to CSV File
SQL Server is a very popular relational database because of its versatility in exporting data in Excel, CSV, and JSON formats. This feature helps with the portability of data across multiple databases. Here, we will learn how to export SQL Server Data from a table to a CSV file. Tools like Azure Dat
3 min read
How to find first value from any table in SQL Server
We could use FIRST_VALUE() in SQL Server to find the first value from any table. FIRST_VALUE() function used in SQL server is a type of window function that results in the first value in an ordered partition of the given data set. Syntax : SELECT *, FROM tablename; FIRST_VALUE ( scalar_value ) OVER
2 min read
SELECT Data from Multiple Tables in SQL
In SQL (Structured Query Language), it is a common requirement to retrieve data from more than one table at once. When you work with relational databases, you often have to combine data from multiple tables to get meaningful results. SQL provides many methods for selecting data from multiple tables,
4 min read
How to find last value from any table in SQL Server
We could use LAST_VALUE() in SQL Server to find the last value from any table. LAST_VALUE() function used in SQL server is a type of window function that results the last value in an ordered partition of the given data set. Syntax : SELECT *, FROM tablename LAST_VALUE ( scalar_value ) OVER ( [PARTIT
2 min read
How to Remove the Last Character From a Table in SQL?
SQL (Structured Query Language) allows for efficient data manipulation and retrieval. A common task in SQL involves removing the last character from a specific column within a table. This can be achieved using string functions like SUBSTRING() and LEN(). In this article, we will demonstrate how to a
4 min read
SQL Server | Convert Tables in T-SQL into XML
XML (Extensible Markup Language) is a widely-used markup language designed to store and transfer structured data between different systems and platforms. While HTML focuses on the visual representation of dataOverviewXML is similar to HTML which is designed to structure and store data for sharing ac
3 min read
SQL Query to Display Nth Record from Employee Table
In database management, retrieving the Nth record is a common operation, especially when dealing with large datasets. Whether we're debugging, analyzing specific rows, or implementing pagination, understanding how to fetch a specific record efficiently is crucial. In analysis, focusing on one partic
6 min read
How to Import and Export SQL Server Database?
Creating and managing a SQL Server database is an essential skill for database administrators and developers. In this article, We will go through the process of setting up a database in SQL Server, from creating the database and tables to inserting records, and finally, exporting and importing the d
3 min read
How to Find Day Name From Date in SQL Server?
Finding the day name from a specific date is a common task in SQL Server, useful for generating reports, analyzing trends, or scheduling. SQL Server provides two primary methods such as the DATENAME() function and the FORMAT() function. In this article, We will learn about How to Find Day Name From
4 min read