Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Query help
Message
From
18/11/2010 17:12:08
 
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01489661
Message ID:
01489682
Views:
48
>>Hi All,
>>
>>Is there a way to avoid using a cursor in order to concatenate "Data" field based on the following table design:
>>
>>ID|Data
>>5 This
>>5 is
>>5 an
>>5 error
>>5 message
>>
>>Expected query result:
>>5 This is an error message
>>
>>Thank you,
>>DANiel
>
>Try
>
>declare @d table(RowID int identity(1,1), ID int, Data varchar(100))
>insert into @d
>select
>5, 'This'
>union all select
>5, 'is'
>union all select
>5, 'an'
>union all select
>5, 'error'
>union all select
>5, 'message'
>
>select ID,  (SELECT Data as [data()]
>FROM @d D1 where D.ID = D1.ID
>order by RowID 
>for xml path(''),TYPE).value('.', 'varchar(max)') as Message
>FROM @d D
>GROUP BY ID
>
>See
>Concatenate rows
>MSDN thread about concatenating rows
>
>Making a list and checking it twice
>
>Concatenating Rows - Part 1
>art
>Concatenating Rows - Part 2

Hang on. The answer to Daniel's question is 'No'. You cheated by adding an extra field to the table :-}
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform