Reputation: 97
I want to keep html tags in my sql query when i write a query to generate xml tags. For example:
select '<p> this is a code</p>' as code
from table name
for xml path (''), type
outputs:
<code><p> this is a code </p> <code>
what it should output:
<code><p> this is a code </p><code>
How do I solve this? Thanks!
Upvotes: 9
Views: 9453
Reputation: 18946
below snippets works for me
DECLARE @HTMLt NVARCHAR(MAX) ;
........
SET @HTMLt = REPLACE(REPLACE(REPLACE(@HTMLt,'&','&' ) , '<','<'),'>','>');
Upvotes: 3
Reputation: 2782
If using xhtml
, I believe the conversion to Xml
will do:
select convert(xml, '<p> this is a code</p>') as code
from table name
for xml path (''), type
EDIT: if the column is ntext
, implicit conversion to Xml
is supported:
create table #t(html ntext)
insert into #t values(N'<p> this is a code</p>')
select convert(xml, html) as code
from #t
for xml path (''), type
drop table #t
Upvotes: 5