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.
Related
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;
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.
This question already has answers here:
Replacing certain character in email addresses with '*' in an SQL query
(3 answers)
Closed 6 years ago.
I have one col in my table like:
COL
Kiyara#ymail.com
Akira#zmail.com
I want my output something like this:
COL
Kxxxxx#ymail.com
Axxxx#zmail.com
iyara replaced with 5x's(xxxxx) and 'kira' with 4x's(xxxx)
There's probably a better way to do it that this, but this achieves what you're after:
DECLARE #email NVARCHAR(50) = 'Kiyara#ymail.com';
SELECT LEFT(#email, 1) + REPLICATE('X',
LEN(SUBSTRING(#email, 2,
CHARINDEX('#', #email) - 1)))
+ SUBSTRING(#email, CHARINDEX('#', #email), LEN(#email));
-- Result: KXXXXXX#ymail.com
This uses the REPLICATE() method, which according to the docs online starts with SQL Server 2008.
REPLICATE (Transact-SQL)
Repeats a string value a specified number of times.
REPLICATE ( string_expression ,integer_expression )
SELECT LEFT(col, 1) + RIGHT('xxxxxxxxxxxxxxxx', CHARINDEX('#', col) - 2) +
SUBSTRING(col, CHARINDEX('#', col), LEN(col) - CHARINDEX('#', col) + 1);
Explanation:
Take Kiyara#ymail.com as an example, and each piece of my query is shown here.
K LEFT(col, 1)
xxxxx RIGHT('xxxxxxxxxxxxxxxx', CHARINDEX('#', col) - 2)
#ymail.com SUBSTRING(col, CHARINDEX('#', col), LEN(col) - CHARINDEX('#', col) + 1)
Note that you can replace the call to RIGHT() with a string of x's long enough to match your longest expected email name.
Try it's
select substring('Akira#zmail.com',1,1) + REPLICATE('x',charindex('#','Akira#zmail.com')) + SUBSTRING('Akira#zmail.com',charindex('#','Akira#zmail.com'),200)
Change the email by column or whatever you want
declare #n nvarchar(max)
set #n='Kiyara#ymail.com'
select concat(substring(#n,1,1),replicate('X',len( substring(#n,2,charindex('#',#n,1)-2))),SUBSTRING(#n,charindex('#',#n),200))
In this with the help o substring you have found first letter ans then rest of he letter til # i.e iyara is searched using charindex ans substring,which will be converted into 'x' using replicate function and then rest id printed.
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 #
I'm trying to generate an email from full name. Following is what I have and I'm stuck here
select distinct tbin.Name
,(SELECT SUBSTRING(tbin.Name, 1, CHARINDEX(' ', tbin.Name) - 1) AS [FirstName])
,(select SUBSTRING(tbin.Name, CHARINDEX(' ', tbin.Name) + 1, 8000) AS LastName)
from tblInitialApplicants tbin
Name is like 'Natalie Marine Baily'. Name can be of any length and also can have any number of space between each part of the name.
Expected result: Natalie.B#gmail.com
'Natalie' - First Name
'B' - First letter of the last name
You can get the position of a char with CHARINDEX, look for the first space and reverse de name and look for the last. With that you can easly substring first and last name.
DECLARE #Fullname VARCHAR(100) = 'Natalie Marine Baily';
select
*,
firstname+'.'+lastnameINITIAL+'#gamil.com'
from (
select
#Fullname Fullname,
SUBSTRING(#Fullname,1,CHARINDEX(' ',#Fullname,1)) firstname,
SUBSTRING(SUBSTRING(#Fullname,LEN(#Fullname)-CHARINDEX(' ',REVERSE(#Fullname),1)+2,CHARINDEX(' ',REVERSE(#Fullname),1)),1,1) lastnameINITIAL
) T
DECLARE #name VARCHAR(200) = 'Natalie Marine Baily'
SELECT LEFT(#name,CHARINDEX(' ',#name,1)-1) + '.'+
LEFT(REVERSE(LEFT(REVERSE(#name),CHARINDEX(' ',REVERSE(#name),1)-1)),1) + '#gmail.com'
Result:
Just see, whether you can work from here.
Your biggest problem is that both first names and last names can have spaces between them. So how will you know where the last name begins? For example Jean-Claude Van Damme.
Also what should the email look like in this case? jean-claude.v#gmail.com?
I would suggests you change your database to store the first and last names as separate fields, or better yet have an email field. Then this will be much easier. If you cannot do that you will have to generate a few possible emails and validate if the emails exist.
How to check if an email address exists without sending an email?
This would be how to generate an email if you take it that the first word is the firstname and the last word is the lastname.
DECLARE #Name varchar(100) = 'Natalie Marine Baily'
select SUBSTRING(#Name, 0, CHARINDEX(' ', #Name) - 1)
+ '.' + SUBSTRING(#Name,LEN( #Name) - CHARINDEX(' ',REVERSE(#Name))+2 , 1) + '#gmail.com'