Monday, October 10, 2005

Concatenate Data Into One Comma Delimited String

To concatenate data from several rows you can use the code below
I have declared the variable as varchar 1000, adjust the size if you think the size doesn't fit your needs

USE pubs

DECLARE @chvCommaDelimitedString VARCHAR(1000)

SELECT @chvCommaDelimitedString = COALESCE(@chvCommaDelimitedString + ',', '') + REPLACE(au_lname,'''','''''')
FROM dbo.authors

SELECT @chvCommaDelimitedString

No comments: