Ruzbeh Irani
Ruzbeh Irani

Reputation: 2438

Build valid HTML from HTML Tags Using SQL

I have a table which contains HTML tag . I just want to create a HTML FORM using this table is HTML FOrmat for eg.

ID Tags
-- ----
1  Html
2  Head
3  Title
4  Meta
5  Body
6  Font

Result should be

ID HTML                                 
-- ------------------------------------------------------------------------------------
1  <Html> <Head> <Title></Title> <Meta></Meta> </Head><Body> <Font></Font></Body</Html>

Upvotes: 0

Views: 206

Answers (1)

Adrian Iftode
Adrian Iftode

Reputation: 15673

declare @t table(id int, tags varchar(50))
insert into @t values 
        (1, 'Html'),
        (2, 'Head'),
        (3, 'Title'),
        (4, 'Meta'),
        (5, 'Body'),
        (6, 'Font')

;with Tags1 as
(
    select xml1 = (select '<' + tags + '>' from @t for xml path (''))
)
,Tags2 as
(
    select xml2 = (select '</' + tags + '>' from @t order by id desc for xml path (''))
)
select replace(replace(Tags1.xml1 + Tags2.xml2,'&lt;','<'),'&gt;','>')
from Tags1, Tags2

Upvotes: 1

Related Questions