Comma Separated Values using COALESCE function in Sql Server 2005

Comma Separated Values using COALESCE function in Sql Server 2005

DECLARE @ItemList VARCHAR(MAX)
SELECT @ItemList = COALESCE(cast(@ItemList as varchar(max)) + ', ', '') + cast(Ratings.RatingType as VARCHAR(100)) + ':' + cast(SiteRating.Count as VARCHAR(100) )
FROM Ratings INNER JOIN
SiteRating ON Ratings.RatingID = SiteRating.RatingID INNER JOIN
Sites ON SiteRating.SiteID = Sites.SiteID
where Sites.SiteID = 4

PRINT @ItemList

Result : Upper:2.75, Middle:3, Lower:2.75

No comments: