Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Query help
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01489661
Message ID:
01489663
Views:
65
This message has been marked as the solution to the initial question of the thread.
>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

Concatenating Rows - Part 2
If it's not broken, fix it until it is.


My Blog
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform