aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 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

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.

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)
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 >
---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) +
return LTRIM(RTRIM(@htmldesc)) 
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


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

Get the latest,, 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


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.

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 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.