Just Code‎ > ‎

T SQL - Select contents of a column into a comma separated string

posted Aug 27, 2009, 1:18 AM by Peter Henell   [ updated Aug 27, 2009, 1:21 AM ]
CREATE Function GetThatTableAsString (@ID INT)
RETURNS varchar(1000)
AS
BEGIN
DECLARE @result varchar(1000)
SET @result = ''
Select @result = @result + RTRIM(ap.value) + ', '
FROM dbo.table ap
WHERE ap.id = @ID

IF @result <> '' SET @result = LEFT(@result , LEN(@result) - 1)

RETURN (@result)
END
GO
Comments