Link to home
Start Free TrialLog in
Avatar of dpav29
dpav29

asked on

One text string instead of multiple rows

Hi,
I am trying to get one string of email addresses seperated by semi-colon so I can paste it into an email address field.  I can get the data in rows:

1  joe@gmail.com
2 sall@verizon.net
3 Mary@yahoo.com

I want one string returned 'joe@gmail.com;sall@verizon.net;Mary@yahoo.com'

I have searched and found some similar things, but all involved PHP or some other programming I'm not familiar with.  I'm using SQL Management Studio 2008 and I am trying to do this using TSQL. . . I thought it would be easy, but. . .not so much.

Thanks in advance for any assistance!
Avatar of Ephraim Wangoya
Ephraim Wangoya
Flag of United States of America image


select  Stuff(
                  (Select  ',' + EmailAddress
                              From    Table1
                              For xml Path('')
                  )                        
                  , 1, 1, '') as Emails
ASKER CERTIFIED SOLUTION
Avatar of Ephraim Wangoya
Ephraim Wangoya
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dpav29
dpav29

ASKER

Wow!  It worked with virtually no effort on my part! :

select Stuff(
             (Select  ';' + pe.Email
      from ProjectEntities pe
      inner join EntityContracts ec
      on pe.ProjectEntityId = ec.ProjectEntityId
      where ec.ContractId = 97
      and pe.Email is not null
      and Not (pe.Email = '')
                     
         For xml Path('')
                  )                        
                  , 1, 1, '') as Emails

Thanks so much for the fast response and perfect solution!
Avatar of dpav29

ASKER

Very quick response . . thank you!