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

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

1 comments :

bazith mohammed 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.

Give your Valuable Comments

Other Related Posts

© 2010-2012 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.