Link to home
Start Free TrialLog in
Avatar of EricLynnWright
EricLynnWrightFlag for United States of America

asked on

One row, seperated by commas for a list

I'm trying to get sql that will return a list of items but in ONE row and each item separated by commas.


Current:

Select col1 from table1
     
Results:
   item1
   item2
   item3


Future:

Results:
   item1, item2, item3
Avatar of Deepak Vasudevan
Deepak Vasudevan
Flag of India image

declare @onerow varchar(max) 
 
;with cte as  
( 
    select distinct item1 + ' ' + item2 + ' ' + item3 as oneName  
    from yourtable yt  

) 
select @onerow = isnull(@onerow + char(13), '') + oneName 
from cte b 
 
print @oneName 

Open in new window

small mistake

declare @onerow varchar(max) 
 
;with cte as  
( 
    select distinct item1 + ', ' + item2 + ' ,' + item3 as oneName  
    from yourtable yt  

) 
select @onerow = isnull(@onerow + char(13), '') + oneName 
from cte b 
 
print @oneName 

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ralmada
ralmada
Flag of Canada 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
SOLUTION
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