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 #
Related
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.
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.
I have a very specific business requirement to hide certain characters within an email address when returned from SQL and I have hit the limit of my ability with SQL to achieve this. I was wondering if someone out there would be able to point me in the right direction. Essentially, my business is asking for the following:
test#email.com to become t*\*t#e**l.com
or
thislong#emailaddress.com to become t******h#e**********s.com
I am aware that if either portion of the email before of after the # are less than 3 characters, then this won't work, but I intend on checking for this and dealing with it appropriately. I have tried a mixture of SUBSTRING, STUFF, LEFT/RIGHT etc but I can't quite get it right.
DECLARE #String VARCHAR(100) = 'example#gmail.com'
SELECT LEFT(#String, 1) + '*****#'
+ REVERSE(LEFT(RIGHT(REVERSE(#String) , CHARINDEX('#', #String) +1), 1))
+ '******'
+ RIGHT(#String, 5)
result will be
e******e#g***l.com
Very interesting and very much tough to generate generic solution try this
this may help you
DECLARE #String VARCHAR(100) = 'sample#gmail.com'
SELECT STUFF(STUFF(#STring,
CHARINDEX('#',#String)+2,
(CHARINDEX('.',#String, CHARINDEX('#',#String))-CHARINDEX('#',#String)-3),
REPLICATE('*',CHARINDEX('.',#String, CHARINDEX('#',#String))-CHARINDEX('#',#String)))
,2
,CHARINDEX('#',#String)-3
,REPLICATE('*',CHARINDEX('#',#String)-3))
OUTPUT will be
s****e#g******l.com
Similar way for thislong#emailaddress.com
OUTPUT will be
t******g#e*************s.com
You could also use regular expressions. Something like this (not completely finished):
select regexp_replace('test#email.com', '^(.?).*#(.?).*', '\1***#\2***')
from dual
Results in:
t***#e***
Could be a useful solution if you can only use SELECT statements.
CREATE FUNCTION dbo.EmailObfuscate
( #Email VARCHAR(255)
) RETURNS TABLE AS RETURN
(
SELECT adr.email
, LEFT (adr.email, 1)
+ REPLICATE ('*', AtPos-3)
+ SUBSTRING (adr.Email, AtPos-1, 3)
+ REPLICATE ('*', Length-DotPos - AtPos - 2)
+ SUBSTRING (adr.Email, Length - DotPos, 10) AS hide
FROM ( VALUES ( #Email) ) AS ADR (EMail)
CROSS APPLY ( SELECT CHARINDEX ('#', adr.Email)
, CHARINDEX ('.', REVERSE(Adr.Email))
, LEN (Adr.Email)
) positions ( AtPos
, dotpos
, Length
)
);
GO
-- Calling
SELECT Emails.*
FROM ( Values ( 'this.long#emailaddress.com')
, ( 'test#email.com' )
, ( 'sample#gmail.com' )
, ( 'test#gmx.de' )
) AS adr (email)
CROSS APPLY dbo.EmailObfuscate (Adr.Email) AS Emails
SELECT
-- Email here is the name of the selected Column from your Table
--Display the First Character
SUBSTRING(Email,1,1)+
--Replace selected Number of *
REPLICATE('*',10)+
--Display the One Character before # along with # & One Character after #
SUBSTRING(Email,CHARINDEX('#',Email)-1,3)+
--Replace selected Number of *
REPLICATE('*',10)+
--Display. Character along with the rest selected Number of Characters
SUBSTRING(Email,CHARINDEX('.',Email)-1, LEN(Email) -
CHARINDEX('.',Email)+12)
--NameEmail is the Table Name
FROM NameEmail
Result is:
j**********l#a**********a.co.uk
I've found this answer, but was searching for PostgreSQL version (and have not found it)
So I've made my own for PGSQL
SELECT
-- Email here is the name of the selected Column from your Table
--Display the First Character
SUBSTRING('g40gj30m#my-email.co.uk', 1, 1) ||
--Replace selected Number of *
REPEAT('*', 10) ||
--Display the One Character before # along with # & One Character after #
SUBSTRING('g40gj30m#my-email.co.uk', strpos('g40gj30m#my-email.co.uk', '#') - 1, 3) ||
--Replace selected Number of *
REPEAT('*', 10) ||
--Display. Character along with the rest selected Number of Characters
SUBSTRING(
'g40gj30m#my-email.co.uk',
strpos('g40gj30m#my-email.co.uk', '.') - 1,
length('g40gj30m#my-email.co.uk') - strpos('g40gj30m#my-email.co.uk', '.') + 12
);
the result will be
?column?
---------------------------------
g**********m#m**********l.co.uk
(1 row)
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'
I am trying to extract the text between two characters using t-sql. I have been able to write it where it pulls the information close to what I want, but for some reason I am not getting what i am expecting(suprise, suprise). Could really use alittle help refining it. I am trying to extract part of the table name that is located between two [ ]. An example of the column data is as follows(this is a table that records all changes made to the database so the column text is basically SQL statements):
ALTER TABLE [TABLENAME].[MYTABLE] ADD
[VIP_CUSTOMER] [int] NULL
I am trying to extract part of the table name, in this example I just want 'MYTABLE'
Right now I am using:
select SUBSTRING(db.Event_Text, CHARINDEX('.', db.Event_Text) + 2, (CHARINDEX(']', db.Event_Text)) - CHARINDEX('', db.Event_Text) + Len(']')) as OBJName
FROM DBA_AUDIT_EVENT DB
WHERE DATABASE_NAME = 'XYZ'
But when I use this, I don't always get the results needed. Sometimes I get 'MYTABLE] ADD' and sometimes I get the part of the name I want, and sometimes depending on the length of the tablename I only get part the first part of the name with part of the name cut off at the end. Is there anyway to get this right, or is there a better way of writing it? Any help would be greatly appreciated. Thanks in advance.
Long, but here's a formula using the brackets:
Declare #text varchar(200);
Select #text='ALTER TABLE [TABLENAME].[MYTABLE] ADD [VIP_CUSTOMER] [int] NULL';
Select SUBSTRING(#text,
CHARINDEX('[', #text, CHARINDEX('[', #text) + 1 ) +1,
CHARINDEX(']', #text, CHARINDEX('[', #text, CHARINDEX('[', #text) + 1 ) ) -
CHARINDEX('[', #text, CHARINDEX('[', #text) + 1 ) - 1 );
Replace #text with your column name.
Give this a shot:
select SUBSTRING(db.Event_Text, CHARINDEX('.', db.Event_Text) + 2
, CHARINDEX(']', db.Event_Text) - 2) as OBJName
FROM DBA_AUDIT_EVENT DB
WHERE DATABASE_NAME = 'XYZ'
this is a pretty ugly way to get the length, but I've used something like this before:
select SUBSTRING(db.Event_Text,
CHARINDEX('.', db.Event_Text) + 2,
charindex('] ADD',db.Event_Text) - CHARINDEX('.',db.Event_Text)-2))
Give it a try, it may work for you.