Extracting Email Domain from string in sql - sql

I have string something like this
'myname' <myname#mydomain.com>
or like this
myname#mydomain.com
I want to extract domain from both kind of strings
For 2nd I can do somthing like this
select RIGHT(email, LEN(email) - CHARINDEX('#', email))
but same is not working for 1st string.
Also I do not know in which format email address will come
I am using Sql Server 2014.

You can use stuff() and replace():
select replace(stuff(email, 1, charindex('#', email), ''), '>', '')
Here is a db<>fiddle.

Related

How to get email domain name from email ID in T-SQL assuming there is more than one delimited value?

I have an email ID column and would like to extract the domain only accounting for sub domains if applicable. My current query is assuming the first delimited value only
For instance 'abc#gmail.com' = gmail
However, if the email is like 'abc#gmail.co.com' it will be extracted as gmail.co - I want it to be gmail only as well
My query:
SUBSTRING(col_email,
CHARINDEX('#', col_email) + 1,
LEN(col_email) - CHARINDEX('#', col_email) - CHARINDEX('.', REVERSE(col_email))) as domain
You can use a few of CHARINDEXs and SUBSTRING for this. This assumes that all email addresses are valid (so have a . after the #) and that you don't have any outlier email addresses like Steve"#"home.has_a_valid_address#mydomain.com:
SELECT SUBSTRING(Email, CHARINDEX('#',Email)+1,CHARINDEX('.',Email,CHARINDEX('#',Email)) - CHARINDEX('#',Email) -1) AS YourDomain
FROM (VALUES('abc#gmail.com'))V(Email)

Extract first and last name from email address. Microsoft SQL Server

I am really new to the SQL environment.
I am now facing a problem that I can't get solved.
I have a field email in which are the email addresses with the following format firstname.lastname#company.com
I now need a select that splits the email into first name, last name so like this.
marc.mueller#blablabla.com
to
Firstname Lastname
marc mueller
I was able to filter out the first name but the last name does not work.
select email,
LEFT(email,CHARINDEX('.', email)-1) AS [New Firstname]
from data
Can someone pls point me in the right direction?
many greetings
Maybe you can try substring function according to the index of "." and "#"
SELECT
SUBSTRING(email,0,charindex('.',email)) as firstname,
SUBSTRING(email,charindex('.',email)+1,charindex('#',email)-charindex('.',email)-1) as lastname FROM data
Firstly, we split the email address according to the "." character and we got the firstname.
Secondly, we split the email address starting from the first "." character to the "#" character.
Remember substring syntax;
SUBSTRING(string, start, length)
So to find the length of the lastname sub string, we should subtract index of "#" from "."
Assuming we always have a single . separating first and last name, and will not appear anywhere else in the value, and also will only have a single # separating name and domain:
select left(left(email, CHARINDEX('#', email) - 1), CHARINDEX('.', email) - 1) first_name
, right(left(email, CHARINDEX('#', email) - 1), len(left(email, CHARINDEX('#', email) - 1)) - CHARINDEX('.', email)) last_name
from tbl
Edit:
Here is a case statement to handle a missing . in the name:
select left(left(email, CHARINDEX('#', email) - 1), CHARINDEX('.', email) - 1) first_name
, case when email like '%.%#%' then right(left(email, CHARINDEX('#', email) - 1), len(left(email, CHARINDEX('#', email) - 1)) - CHARINDEX('.', email)) else left(left(email, CHARINDEX('#', email) - 1), CHARINDEX('.', email) - 1) end last_name
from tbl
However, we have no way of knowing if it is the first or last name, so this will return the name in both columns.
And another alternative using parsename. CTEs are used to help "see" the logic flow but it can all be converted into a more compact form.
declare #x table (email varchar(200));
insert #x (email) values ('marc.mueller#blablabla.com'), ('lastname#company.com')
;
with cte_replace as (select replace(email, '#', '.') as p1
from #x
),
cte_split as (select parsename(p1, 4) as p2, parsename(p1, 3) as p3
from cte_replace
)
select * from cte_split;
Notice how the logic handles a name without a period to separate first/last parts. An alternative to the REPLACE usage is to simple truncate from the # sign.

How trim() works in SQL Server

I have a column SysTraNo with some specific data like
HO/20-21/DRP/0001
215/21-22/AGP/0003
I want to trim that whole column and only take 20-21 or 21-22 of that data. How can I do this?
Using the base string functions we can try:
SELECT val,
SUBSTRING(val,
CHARINDEX('/', val) + 1,
CHARINDEX('/', val, CHARINDEX('/', val) + 1) -
CHARINDEX('/', val) - 1) AS nums
FROM yourTable;
Demo
The logic here is to take a substring starting from the character after the first / until the character before the second /. On other databases, we could have used regular expressions, but SQL Server has no native support for this.
In sql server trim works like below.
As one example is above Here I have given another example using substring on email to extract the domain part.
SELECT
email,
SUBSTRING(
email,
CHARINDEX('#', email)+1,
LEN(email)-CHARINDEX('#', email)
) domain
FROM
practice
ORDER BY
email;

Extract Domain text from EmailAddress

I am hoping to find a way to extract espn from abcd#espn.com
I am working in Microsoft SQL Server. Does anyone know how to write the substring to accomplish this?
Thanks!!
try this
select SUBSTRING(
'xx.abcd#espn.com', --input string
charindex('#', 'xx.abcd#espn.com') + 1,
(len('xx.abcd#espn.com')-charindex('#', 'xx.abcd#espn.com'))-4
)
DECLARE #emailAddress TABLE (emailAddress VARCHAR(255))
INSERT #emailAddress (emailAddress)
VALUES ('abcd#espn.com'),
('ab.cd#taylormade.edu'),
('notanemailaddress')
SELECT IIF(emailAddress LIKE '%#%.%', -- Confirm it has valid structure
SUBSTRING(emailAddress,
CHARINDEX('#',emailAddress)+1,
CHARINDEX('.',emailAddress,CHARINDEX('#',emailAddress))-(CHARINDEX('#',emailAddress)+1)), --Extract the domain
NULL) -- Provide default value if not an emailAddress
AS emailDomain
FROM #emailAddress
This accounts for values that are not valid emailAddresses and returns a NULL (or whatever you put there) when an invalid value is encountered.
This should work:
select SUBSTRING(
#email, --input string
charindex('#', #email) + 1, --character just after the #
charindex('.', #email, charindex('#', #email)) - charindex('#', #email) - 1 --distance between # and the first . following it
)
You're looking for this.
Select substring(#email,charindex('#', #email)+1,
charindex('.',right(#email,len(#email) - charindex('#', #email)))-1)
Email addresses can contain . multiple times before and after the #, this should return the first part of the domain after the #

How to replace all the dots before # in an email with empty string in Oracle SQL?

I want to replace all the dots before # in an email with empty string in oracle query
like:
anurag.mart#hotmail.com >> anuragmart#hotmail.com
Instr - To identify the position(#)
Substr - To extract data between start(1) and end(#) position
Replace - To replace . with ''
|| - To concatenate two strings
Try this
SELECT Replace(Substr('anurag.mart#hotmail.com', 1,
Instr('anurag.mart#hotmail.com', '#', 1)), '.', '')
|| Substr('anurag.mart#hotmail.com', Instr('anurag.mart#hotmail.com','#')+1)
FROM dual
Result:
anuragmart#hotmail.com
SqlFiddle Demo
The easiest way is to use REGEXP_REPLACE to identify the pattern and replace it with required pattern.
regexp_replace('anurag.mart#hotmail.com', '(\w+)\.(\w+)(#+)', '\1\2\3')
For example,
SQL> SELECT 'anurag.mart#hotmail.com' email_id,
2 regexp_replace('anurag.mart#hotmail.com', '(\w+)\.(\w+)(#+)', '\1\2\3') new_email_id
3 FROM dual;
EMAIL_ID NEW_EMAIL_ID
----------------------- ----------------------
anurag.mart#hotmail.com anuragmart#hotmail.com
I came on this page while looking for solutions for SQL servers, I converted the above for SQL server for my project, Here is SQL if anybody else needs it.
SELECT
CONCAT(
REPLACE(
SUBSTRING(EmailAddress, 1, CHARINDEX('#', EmailAddress)-1),
'.',
''
),
SUBSTRING(EmailAddress, CHARINDEX('#', EmailAddress), LEN(EmailAddress))
)
FROM [Applicant]