Problem:
How to flatten rows in a column into a single row.?
Solution:
Using For XML to through the rows
and append to a single row.
Example:
Example 1: In this example I would
like to show you how we can concatenate emails in multiple rows to single row.
This can be helpful when you want to send alerts to all people in a particular
table.
DECLARE @emails table
(
email char(32)
)
insert into @emails
values ('A@DOMAIN.com')
, ('B@Domain.com')
, ('C@Domain.com')
, ('D@Domain.com')
SELECT
(SELECT rtrim(email) + ';' FROM @emails FOR XML PATH('')) emailList
OUTPUT:
0 comments:
Post a Comment