Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Need help with T-SQL to combine email addresses Expand / Collapse
Author
Message
Posted Friday, August 2, 2013 12:04 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, September 22, 2014 10:01 AM
Points: 105, Visits: 208
Hello,

I am trying to get email addresses in column and concatenate them into a long string, separated by semi-column ";". So, the results would be like:

bob@company.com; mary@company.org;paul@company.com;susan@company.net

I have this T-SQL below and I am using a while loop that does not work, but I am wondering if I a cursor would work better. I am not sure, as what is below is not working. Can you help me with this query to get the results?

declare @emails as varchar(5000)
while exists (select People.cEmail from dbo.People)
begin
set @emails = cEmail + ';' + @emails
end

Thanks.
Post #1480551
Posted Friday, August 2, 2013 12:07 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 3:51 PM
Points: 3,784, Visits: 8,488
There's no need for a cursor or a while loop. Check the following article to solve your problem.
http://www.sqlservercentral.com/articles/comma+separated+list/71700/



Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1480554
Posted Friday, August 2, 2013 12:16 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:04 PM
Points: 13,086, Visits: 12,553
If you don't need any grouping and just simply want all emails from that table into a single column you can do that like this.

select People.cEmail 
from dbo.People
for xml path('')

If however, you need other columns from that table or some other information the article that Luis pointed you to is an excellent start.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1480563
Posted Friday, August 2, 2013 12:36 PM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Today @ 7:18 AM
Points: 612, Visits: 2,860
You don't need a loop to do what you were trying to do. This would do the trick

DECLARE @emails varchar(5000)=''

SELECT @emails=@emails+';'+cEmail
FROM dbo.people;

This technique, however, can give you data issues... The prefered method would be to use FOR XML PATH as discussed in the link Louis provided....

SELECT x FROM 
( SELECT cEmail + ';'
FROM #people
FOR XML PATH('')) xx(x)



-- Alan Burstein



Read this article for best practices on asking questions.
Need to split a string? Try this (Jeff Moden)
Need a pattern-based string spitter? Try this (Dwain Camps)

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001

My blog
Post #1480567
Posted Friday, August 2, 2013 12:40 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 1:04 PM
Points: 13,086, Visits: 12,553
heh I forgot the semicolon in my post. Alan you don't need to do this with a subselect, just a simple query works.

SELECT cEmail + ';'
FROM #people
FOR XML PATH('')



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1480568
Posted Friday, August 2, 2013 12:49 PM


SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Today @ 7:18 AM
Points: 612, Visits: 2,860
Sean Lange (8/2/2013)
heh I forgot the semicolon in my post. Alan you don't need to do this with a subselect, just a simple query works.

SELECT cEmail + ';'
FROM #people
FOR XML PATH('')



Thanks.


-- Alan Burstein



Read this article for best practices on asking questions.
Need to split a string? Try this (Jeff Moden)
Need a pattern-based string spitter? Try this (Dwain Camps)

"I can't stress enough the importance of switching from a 'sequential files' mindset to 'set-based' thinking. After you make the switch, you can spend your time tuning and optimizing your queries instead of maintaining lengthy, poor-performing code. " -- Itzek Ben-Gan 2001

My blog
Post #1480574
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse