Pages

Social Icons

Sunday 23 October 2011

Comma separated Value In SQL-Server


Some times we required a column values in a row as comma separated for example
Column1
---------
---------
A1
A2
A3
A4
A5
---------
is like : A1, A2, A3, A4, A5
I n sql we can get this type of result like this :



DECLARE @A VARCHAR(MAX)
SET @A = 
(
SELECT 
LTRIM(RTRIM(t1.column1))+ ', '   
FROM 
Temp1 t1 WITH(NOLOCK) 
INNER JOIN 
Temp2 t2
ON 
t1.column1 = t2.column2
FOR XML PATH('')     
)


SELECT @A


No comments:

Post a Comment