Question

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!

Was it helpful?

Solution

You can try the following :

DECLARE @email VARCHAR(8000)  
SELECT @email = COALESCE(@email + ';', '') + [Email Address] FROM MYTABLE WHERE ID='ABC' 
SELECT @email
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top