Wednesday, September 28, 2011

how to remove html tags and xml tags in sql server 2008 ( Best way to strip html tags from a string in sql server )

Function:
create function dbo.StripHTML(@text varchar(max))
returns varchar(max) 
as
begin
    declare @textXML xml
    declare @result varchar(max)

    set @textXML = @text;

    with doc(contents) as

    (
        select chunks.chunk.query('.') from  
         @textXML.nodes('/') as chunks(chunk)
    )
    select @result = contents.value('.', 'varchar(max)') from doc

    return @resultend
go
 

select dbo.StripHTML('This <i>is</i> an <b>html</b> test')
 
Output :
 This is an html test



How to remove html and xml tags

DECLARE @htmlXmlTags VARCHAR(1000);

SET @htmlXmlTags='<?xml version="1.0" encoding="utf-16"?><HTML><BODY>
<P STYLE="font-size:16;font-family:Calibri;
color:#000000;font-weight:normal;font-style:normal;
text-align:Left;"><SPAN>
This <i>is</i> an <b>html</b> test</SPAN>
</P></BODY></HTML>'; 

 SELECT dbo.StripHTML(Replace(@htmlXmlTags,'
<?xml version="1.0" encoding="utf-16"?>' , ''));


Output :
 This is an html test


Contact: thulasiram.eee@gmail.com
 

No comments:

Post a Comment