23 January 2012

SQL: Concatinate Rows into a string without using variables

Hi All,

Here is a small SQL script which allows you to concatenate the values from all rows into a single row/value.


SELECT T.value + ',' AS [text()]

FROM myTable T
FOR XML PATH ('')


Nothing difficult.
Previously I was  doing that using a variable, something like this:

DECLARE @var NVARCHAR(MAX)SET @var = ''
SELECT @var = @var + T.value + ','FROM myTable T
SELECT @var

But why doing it so if there is a simpler way.

Regards,
Roman

No comments:

Post a Comment

your thoughts are welcome:

Need more? Leave comments and subscribe to my blog.

.