1

I am working with SQL Server 2008 R2.

This is my table:

CREATE TABLE step
(
   id int IDENTITY(1,1) NOT NULL PRIMARY KEY,
   step_value nvarchar(max) NOT NULL
)

Currently there are 197 rows in step table. I want to get all values of step_value column as a single value with a semi-colon delimiter. How can I do that?

Some sample data:

Insert Into step(step_value)
Values ('a'),('b'),('c'),('d'),('<workflow name="DISCIPLINE_LETTER">'), ('   <target>MEETING_INITIAL_MEETING_LETTER</target> ')

Thanks

srh
  • 1,661
  • 4
  • 30
  • 57

2 Answers2

0

Use STUFF function

select stuff(
 (select '; ' + step_value 
 from step
 for xml path(''), root('MyString'), type
 ).value('/MyString[1]','nvarchar(max)')
, 1, 2, '') as step_values ;

FIDDLE

Mihai
  • 26,325
  • 7
  • 66
  • 81
0
SELECT TOP 1 (STUFF((SELECT ', ' + CAST(id AS VARCHAR(MAX)) 
         FROM step 
         FOR XML PATH ('')), 1, 2, '')) AS a
FROM step
PollusB
  • 1,726
  • 2
  • 22
  • 31