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.
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:
In
previous articles I written Can function return multiple values in SQL Server, SQL Server split function to split string with comma, SQL query to get data between two
characters range, SQL Server reset identity column to 1
in database,
SQL Server acid properties and many articles
related to SQL
Server.
Now I will explain how to remove html tags 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)
as
begin
declare @first int, @last int,@len int
set @first = CHARINDEX('<',@htmldesc)
set @last = CHARINDEX('>',@htmldesc,CHARINDEX('<',@htmldesc))
set @len = (@last - @first) + 1
while @first >
0 AND @last >
0 AND @len >
0
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) + 1
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. |
|||
|
|||
4 comments :
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.
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.
How can I also remove the following '& n b s p ' using the above script?
Note: Only a member of this blog may post a comment.