Wednesday, 26 March 2014

Get comma separated values in SQL Server

DECLARE @Result VARCHAR(MAX)
SET @Result = ''   
SELECT @Result = @Result +COLUMN_NAME + ','
FROM dbo.TABLE_NAME        
IF(LEN(@Result) > 0 )
SELECT @Result = SUBSTRING(@Result,1,LEN(@Result)-1)
-- Return the result of the function
SELECT @Result as COLUMN_NAME