how to fetch mutiple rows as comma seperated single coloumn

Posted by Amritha444 under Sql Server on 7/12/2013 | Points: 10 | Views : 709 | Status : [Member] | Replies : 3
hi all

"select Comment from table_Comments where Id=4 " this query reruns

comments
abc
def
ghi

i want a query which returns comments separated by comma like

comments
abc,def,ghi

how to write such a query

Thanks in advance
Amritha




Responses

Posted by: Pandians on: 7/12/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
DECLARE @Data VARCHAR(MAX)

SELECT @Data = COALESCE(@Data +',','') + Comment FROM table_Comments WHERE Id=4
SELECT @Data


Cheers
www.SQLServerbuddy.blogspot.com
iLink Multitech Solutions

Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Amritha444 on: 7/13/2013 [Member] Starter | Points: 25

Up
0
Down
hi
thanks for the replu
but i really want the following code
SELECT empID,
SUBSTRING(
(
SELECT ( ', ' + EmpName)
FROM TempTable1 t2
WHERE t1.empID = t2.empID
ORDER BY t1.empID, t2.empID
FOR XML PATH('')
), 3, 1000)
FROM TempTable1 t1
GROUP BY empID


Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Venkat0454 on: 7/13/2013 [Member] Starter | Points: 25

Up
0
Down
DECLARE @Comments VARCHAR(MAX)


SELECT @Comments = COALESCE(@Comments +',','') + Comment FROM table_Comments where Id=4

SELECT @Comments


Amritha444, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response