SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Parse out all email addresses in a string
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Rich75
Starting Member

8 Posts

Posted - 07/23/2013 :  12:22:28  Show Profile  Reply with Quote
I have a string like this one in my column

Mike@yahoo.com, Bill@aol.com, Dan@yahoo.com, Frank@gmail.com

In my result set I need to display all email addresses that do not have the @yahoo.com domain. Could anyone suggest how to do this?

Thanks

SwePeso
Patron Saint of Lost Yaks

Sweden
30265 Posts

Posted - 07/23/2013 :  12:37:14  Show Profile  Visit SwePeso's Homepage  Reply with Quote
How do you suggest we write the code so the display thingy is according to your idea?


Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

MuMu88
Aged Yak Warrior

547 Posts

Posted - 07/23/2013 :  12:50:04  Show Profile  Reply with Quote
Is this what you are looking for:


-- TEST DATA
DECLARE @Temp TABLE(email VARCHAR(60));
INSERT INTO @Temp VALUES
('Mike@yahoo.com'),
('Bill@aol.com'), 
('Dan@yahoo.com'),
('Frank@gmail.com');


SELECT * from @Temp where email not like '%@yahoo.com';

Go to Top of Page

MuMu88
Aged Yak Warrior

547 Posts

Posted - 07/23/2013 :  13:04:25  Show Profile  Reply with Quote
If you have a comma separated list may be this solution:



DECLARE @s VARCHAR(255) =  'Mike@yahoo.com, Bill@aol.com, Dan@yahoo.com, Frank@gmail.com';
SELECT STUFF(c,1,1,'') AS emails
FROM 
(
 SELECT ','+Item FROM 
  dbo.DelimitedSplit8K(@s,',')
 WHERE
  Item NOT LIKE '%@yahoo.com'
 FOR XML PATH('')
) T(c);




EDITED: Source code for dbo.DelimitedSplit8K() can be found here:
Figure 21 in this article: http://www.sqlservercentral.com/articles/Tally+Table/72993/

Edited by - MuMu88 on 07/23/2013 13:08:58
Go to Top of Page

Rich75
Starting Member

8 Posts

Posted - 07/23/2013 :  14:03:55  Show Profile  Reply with Quote
quote:
Originally posted by MuMu88

If you have a comma separated list may be this solution:



DECLARE @s VARCHAR(255) =  'Mike@yahoo.com, Bill@aol.com, Dan@yahoo.com, Frank@gmail.com';
SELECT STUFF(c,1,1,'') AS emails
FROM 
(
 SELECT ','+Item FROM 
  dbo.DelimitedSplit8K(@s,',')
 WHERE
  Item NOT LIKE '%@yahoo.com'
 FOR XML PATH('')
) T(c);




EDITED: Source code for dbo.DelimitedSplit8K() can be found here:
Figure 21 in this article: http://www.sqlservercentral.com/articles/Tally+Table/72993/



Thanks a lot
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

Sweden
30265 Posts

Posted - 07/23/2013 :  14:28:03  Show Profile  Visit SwePeso's Homepage  Reply with Quote
DECLARE @Sample VARCHAR(MAX) = 'Mike@yahoo.com, Bill@aol.com, Dan@yahoo.com, Frank@gmail.com';

SELECT		f.n.value('.', 'VARCHAR(MAX)') AS Email
FROM		(
			SELECT	CAST('<m>' + REPLACE(@Sample, ', ', '</m><m>') + '</m>' AS XML) AS Data
		) AS d
CROSS APPLY	d.Data.nodes('m') AS f(n)
WHERE		f.n.value('.', 'VARCHAR(MAX)') NOT LIKE '%@yahoo.com';



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 07/23/2013 :  14:32:44  Show Profile  Reply with Quote
quote:
Originally posted by SwePeso

DECLARE @Sample VARCHAR(MAX) = 'Mike@yahoo.com, Bill@aol.com, Dan@yahoo.com, Frank@gmail.com';

SELECT		f.n.value('.', 'VARCHAR(MAX)') AS Email
FROM		(
			SELECT	CAST('<m>' + REPLACE(@Sample, ', ', '</m><m>') + '</m>' AS XML) AS Data
		) AS d
CROSS APPLY	d.Data.nodes('m') AS f(n)
WHERE		f.n.value('.', 'VARCHAR(MAX)') NOT LIKE '%@yahoo.com';



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA

While insignificant for a single variable such as this, if the intent is to extend this to large amounts of data (stored in a table), constructing and then shredding the XML this way would be much more resource intensive than the method Mumu posted.
Go to Top of Page

madhivanan
Premature Yak Congratulator

India
22761 Posts

Posted - 07/28/2013 :  11:41:22  Show Profile  Send madhivanan a Yahoo! Message  Reply with Quote
Normalise the table and store each email as separate row so that your query is so easy like

where email not like '%@yahoo.com'

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.08 seconds. Powered By: Snitz Forums 2000