Reputation: 5787
Using the tutorial here:
http://www.sqlservercurry.com/2008/06/combine-multiple-rows-into-one-row.html
I wrote the following query:
SELECT DISTINCT STUFF( (SELECT '<li>' + CountryCode + '</li>' from Country FOR XML PATH('')),1,1,'') as Codes FROM Country
This works great, but all my HTML is now encoded (i.e. <li>
)
Is there a way around this? Using a SUBSTRING() approach does this also.
Upvotes: 3
Views: 2821
Reputation: 134933
it is because XML PATH makes it xml safe so <
becomes <
try
DECLARE @CountryCode VARCHAR(MAX)
SELECT @CountryCode = ''
SELECT @CountryCode = @CountryCode + '<li>' + CountryCode + '</li>' from Country
SELECT @CountryCode
example
DECLARE @CountryCode VARCHAR(MAX)
SELECT @CountryCode = ''
SELECT @CountryCode = @CountryCode + '<li>' + name + '</li>'
FROM master..spt_values
WHERE type = 'a'
SELECT @CountryCode
you can also of course use the REPLACE
function to replace '<
' to '<'
In my opinion adding li
should be done at the front end and not in the database
Upvotes: 3
Reputation: 138960
Change '<li>' + CountryCode + '</li>'
to CountryCode as li
and you will probably get what you want.
Upvotes: 4