In sql server using stuff  the string replaced when its  have tag “<” or “>” then  &gt;  when its  “&” then &amp;

Example

select
stuff(
(select ‘, <‘ + name + ‘>’
from sys.databases
where database_id > 4
order by name
for xml path(”)
)
, 1, 2, ”) as namelist;

For avoid replace  &gt; or &amp;

 

select
stuff(
(select ‘, <‘ + name + ‘>’
from sys.databases
where database_id > 4
order by name
for xml path(”), root(‘MyString’), type
     ).value(‘/MyString[1]’,’varchar(max)’)
, 1, 2, ”) as namelist;

 

 

Advertisements