Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate column values as single value SQL Server 2005

I have a table with a column:

|-------------|
| ColumnName  |
|-------------|
| Value One   |
| Value Two   |
| Value Three |
| Value Four  |
| Value Five  |
|-------------|

I will declare a variable,

DECLARE @ColumnNameList VARCHAR(MAX)

and I should find the below value in my variable

'Value One,Value Two,Value Three,Value Four,Value Five'

Can anyone help me out in doing this?

Thanks in advance.

like image 880
user2717449 Avatar asked Aug 26 '13 09:08

user2717449


1 Answers

SELECT @ColumnNameList = STUFF 
(
    SELECT ',' + ColumnName  
    FROM TableName
    ORDER BY ColumnName  
    FOR XML PATH('root')
), 1, 1, ''
like image 165
Giannis Paraskevopoulos Avatar answered Sep 21 '22 05:09

Giannis Paraskevopoulos