0

I have table where content have html values and trying to concatenate them with

<ul> <li>

I have used below query

CREATE TABLE #T(Value varchar(1000))
INSERT INTO #T
values('<p><b>AA</b> something 1</p>'),('<p><b>BB</b> something 2</p>'),('<p><b>CC</b> something 3</p>')

select *
from #T


SELECT '<ul>' +STUFF((SELECT '<li>' + Value + '</li>' 
              FROM #T
              FOR XML PATH('')), 1, 0, '') + '</ul>' AS Notes

Is there anyway to get the HTML as it is, so output will be as below

<ul>
<li><p><b>AA</b> something 1</p></li>
<li><p><b>BB</b> something 2</p></li>
<li><p><b>CC</b> something 3</p></li>
</ul>

Not like

<ul>&lt;li&gt;&lt;p&gt;&lt;b&gt;AA&lt;/b&gt; something 1&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;&lt;b&gt;BB&lt;/b&gt; something 2&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;&lt;b&gt;CC&lt;/b&gt; something 3&lt;/p&gt;&lt;/li&gt;</ul>
4
  • 1
    What language are you using the deliver the data from the database to the client side? Commented Dec 18, 2017 at 10:01
  • Could you try renaming your question? This is not going to attract any help to your question and if this gets a good answer people will not be able to find it in the future because of this title Commented Dec 18, 2017 at 10:12
  • Reason for down vote? Commented Dec 18, 2017 at 10:44
  • @JohnBell : will be using C# Commented Dec 18, 2017 at 10:50

1 Answer 1

3

You getting the results like this because there is no > and < symbols (As well as Ampersand, Double Quotes, and Apostrophy ) Can't be stored in XML as it is, so it will be converted to some codes like &lt ; for <.

Just wrap Notes with a replace function to replace the < and > symbols

CREATE TABLE #T(Value varchar(1000))
INSERT INTO #T
values('<p><b>AA</b> something 1</p>'),('<p><b>BB</b> something 2</p>'),('<p><b>CC</b> something 3</p>')

;with html
as
(
    SELECT '<ul>' +STUFF((SELECT '<li>' + Value + '</li>' 
              FROM #T
              FOR XML PATH('')), 1, 0, '') + '</ul>' AS Notes
)
select
    Notes = REPLACE(REPLACE(Notes,'&lt;','<'),'&gt;','>')
    from html
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.