Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

How to Remove HTML Tags from String in SQL Server

Apr 2, 2014
Introduction:

Here I will explain how to remove html tags from string in
SQL Server or how to parse html tags and retrieve only text from string in SQL Server without using regular expressions or remove text between < and > and get only text from string in SQL Server.
Description:

To implement this functionality we need to create one user defined function to parse html text and return only text

Function to replace html tags in string


CREATE FUNCTION [dbo].[fn_parsehtml] 
(
@htmldesc varchar(max)
) 
returns varchar(max)
as
begin
declare @first int, @last int,@len int 
set @first = CHARINDEX('<',@htmldesc) 
set @last = CHARINDEX('>',@htmldesc,CHARINDEX('<',@htmldesc)) 
set @len = (@last - @first) +
while @first > 0 AND @last > 0 AND @len >
begin 
---Stuff function is used to insert string at given position and delete number of characters specified from original string
set @htmldesc = STUFF(@htmldesc,@first,@len,'')  
SET @first = CHARINDEX('<',@htmldesc) 
set @last = CHARINDEX('>',@htmldesc,CHARINDEX('<',@htmldesc)) 
set @len = (@last - @first) +
end 
return LTRIM(RTRIM(@htmldesc)) 
end 
Once we create function run the query like as shown below


select dbo.fn_parsehtml('<p style="margin: 0px 0px 20px; padding: 0px; color: #333333; ">If you are using an identity column on your SQL Server tables, you can set the next insert value to whatever value you want.</p>  <p style="margin: 20px 0px; ">It would be wise to first check </p>  ')
Once we run above query output will be like as shown below

Output:


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

4 comments :

Bazith said...

Hai Suresh,

This nice article. I face this situation. Let me know if we have the content like "age > 10 ". How can i manage this. Give your valuable suggestion.

Thanks,
B.Bazith Mohammed.

Anonymous said...

Hi Suresh , on executing this function to column of table, i am facing error

Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

Unknown said...
This comment has been removed by the author.
Unknown said...

How can I also remove the following '& n b s p ' using the above script?

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.