Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi all

SQL
select Comment from table_Comments where Id=4

Above query returns:
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
Posted
Updated 12-Jul-13 6:43am
v2
Comments
Maciej Los 12-Jul-13 12:44pm    
What have you tried so far? Where are you stuck?
Use "Improve question" widget to update your question.

A similar kind of question was asked before.

If this is your table:

VB
No Name
1 aaa
1 bbb
2 ccc
1 ddd
3 eee
2 fff


Here goes the query:

SQL
SELECT No,
		SUBSTRING(
		(Select (',' + Name)
		from Temp_Table t2
		where t1.No=t2.No
		order by
		No,
		Name
		FOR xml path( '' ) ),3,1000)
		FROM Temp_Table t1
		group by No
 
Share this answer
 
hi all
thanks for the response
what im looking for is following
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900