August 2, 2013 at 12:04 pm
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.
August 2, 2013 at 12:07 pm
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/
August 2, 2013 at 12:16 pm
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
August 2, 2013 at 12:36 pm
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)
-- Itzik Ben-Gan 2001
August 2, 2013 at 12:40 pm
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 Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
August 2, 2013 at 12:49 pm
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.
-- Itzik Ben-Gan 2001
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply