How to merge data row into an object.
For example :
Declare @Email as varchar(max);
Select @Email=email from M_Employee
I want to merge all email into @Email with ';' as separator.
How to merge data row into an object.
For example :
Declare @Email as varchar(max);
Select @Email=email from M_Employee
I want to merge all email into @Email with ';' as separator.
Try this
select
@Email=LEFT(l.list,LEN(l.list)-1)
FROM
(
SELECT Email + '; ' AS [text()]
FROM M_Employee
FOR XML PATH('')
)l(list)
You you can use STUFF
, with FOR XML
:
SELECT @Email = STUFF((SELECT distinct ';' +
email
FROM M_Employee AS t
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
, 1, 1, '');
This will give you all the emails comma separated with ;
.