1

I have to extract just domain name in email without domain extension. Example I have email address katad@hotmail.com and I need just hotmail as output so I can compare with known and accepted domain list.

I tried to use this but it's not output I was hoping for:

SUBSTRING(subscribers_email,(CHARINDEX('@',subscribers_email)+1),(CHARINDEX('.',subscribers_email))) 

2nd example: I have email as

d.katana@us.army.mil

, I will need

us

as domain and when comparing to domain list will result as valid. 3rd example: I have email as Dra.Katana@us.army.mil, I will need

us and when compared it should be valid, currently it would be null as domain for some reason.

DKCroat
  • 347
  • 2
  • 7
  • 15
  • And what if the email address contains a sub-domain? – Dale K Apr 21 '21 at 19:53
  • That is certainly great question, I have not come across any emails with sub-domains as of yet, not sure exactly what I should do in that case. :) – DKCroat Apr 21 '21 at 19:57

4 Answers4

1

One more solution.

SQL

DECLARE @email VARCHAR(100)='katad@hotmail.com';

SELECT @email AS email
    ,  PARSENAME(REPLACE(@email, '@', '.'), 2) AS domain;

Output

+-------------------+---------+
|       email       | domain  |
+-------------------+---------+
| katad@hotmail.com | hotmail |
+-------------------+---------+

SQL #2

-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, email VARCHAR(100));
INSERT INTO @tbl (email) VALUES 
('katad@hotmail.com'),
('john.m.cappelletti@sub.domain.com'),
('d.katana@us.army.mil'),
('dra.m.katanad@gmail.com'),
('drazen.i.katanic@abc.com');
-- DDL and sample data population, end

SELECT *
    , PARSENAME(SUBSTRING(email, CHARINDEX('@', email) + 1, 100), 2) AS domain
FROM @tbl;

Output

+----+-----------------------------------+---------+
| ID |               email               | domain  |
+----+-----------------------------------+---------+
|  1 | katad@hotmail.com                 | hotmail |
|  2 | john.m.cappelletti@sub.domain.com | domain  |
|  3 | d.katana@us.army.mil              | army    |
|  4 | dra.m.katanad@gmail.com           | gmail   |
|  5 | drazen.i.katanic@abc.com          | abc     |
+----+-----------------------------------+---------+
Yitzhak Khabinsky
  • 18,471
  • 2
  • 15
  • 21
0

This works for your example, you can add additional replace clauses if needed for other top-level names

declare @email varchar(100)='katad@hotmail.com'
select Replace(Right(@Email, Len(@Email) - CharIndex('@', @email)),'.com','')
Stu
  • 30,392
  • 6
  • 14
  • 33
  • so with this I would get hotmailcom, so I need just hotmail, .com or .net or anything else needs to be removed – DKCroat Apr 21 '21 at 19:41
0

Here is a solution that returns the first level domain regardless of any sub-domains:

Declare @testData Table (email varchar(100));
 Insert Into @testData (email)
 Values ('katad@hotmail.com'), ('john.m.cappelleti@sub.domain.com'), ('Dra.Katana@us.army.mil'), ('d.katana@us.army.mil');

 Select *
      , domain = substring(td.email, p1.pos, p2.pos - p1.pos)
   From @testData                                               As td
  Cross Apply (Values (charindex('@', td.email, 1) + 1))        As p1(pos)
  Cross Apply (Values (charindex('.', td.email, p1.pos)))       As p2(pos);

Results from sample table:

enter image description here

Jeff
  • 512
  • 2
  • 8
0

You need to start the search from after the @

CROSS APPLY (VALUES works wonders for holding intermediate values

SELECT
    SUBSTRING(subscribers_email, v1.atsign, v2.dot - v1.atsign) 
FROM table
CROSS APPLY (VALUES (NULLIF(CHARINDEX('@', subscribers_email), 0) ) ) v1(atsign)
CROSS APPLY (VALUES (NULLIF(CHARINDEX('.', subscribers_email, v1.atsign), 0) + 1 ) ) v2(dot)
Charlieface
  • 52,284
  • 6
  • 19
  • 43