I am working with an organization that does not have AD distribution groups. SQL is 2012. One table has rows of an identifier and then a second row has the corresponding email addresses. I wanted to run a script to grab the email addresses based off the identifier and create one string separated by semi colons.

So:

SELECT [Email Address] FROM MYTABLE WHERE ID="ABC" 

Result:
123@email.com
345@email.com
567@email.com

I want result of 123@email.com;345@email.com;567@email.com

What would work best, recursive, pivot, SQL Concatenation?

I'd like something fairly straight forward and am just not sure what the best method is. Any tips or advice is greatly appreciated. Thank you!

有帮助吗?

解决方案

You can try the following :

DECLARE @email VARCHAR(8000)  
SELECT @email = COALESCE(@email + ';', '') + [Email Address] FROM MYTABLE WHERE ID='ABC' 
SELECT @email
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top