JANA GANA MANA

Promoting indian languages
search BLOGS
Login
Add your comments
Want to contribute?
Join JANAGANAMANA



Print this page.




How to get a column values in a single Row

- SQL

jagan(8/22/2008)
Problem: select name from Mytable 
I want all 'name's in a single row , selperated by ','
test(8/22/2008)
select isnull(name,' ') + ',' AS  [text()] from dbo.Mytable 
FOR XML PATH ('') 

Only from SQL server 2005 +...
test(8/22/2008)
Here is for adding other column with group by 

select re.col2 ,count(*)  
,(

select isnull(rei.name,' ') + ',' AS  [text()] from dbo.Mytable rei
 WHERE (  
          (    
               (re.col2 is NULL) 
                AND 
               (rei.col2 is NULL)  
          )  
          OR
          ( re.col2  = rei.col2 ) 
       )
FOR XML PATH ('') 

) as  names
from dbo.Mytable re
group by re.col2  
jagan(8/22/2008)
Thanks a lot .....