I saw a lot of examples using COALESCE and others “methods” for this purpose .Here is one of them:
@result nvarchar(1024)
SELECT @result = COALESCE(@result + ',', '') + column_name
FROM some_table
expected: [value,value,value]
result:[value , value , value]
Has anybody this problem or I must find my mistake in something else?
It sounds like you might have whitespace in the
column_namevalues. You should useLTRIM()andRTRIM()around thecolumn_nameto remove any whitespace.:see SQL Fiddle with Demo
Or you can use
REPLACE()on the final result to remove whitespaces:see SQL Fiddle with Demo