How to take away HTML Tags from String in SQL Server

Here i'll make a case for the way to take away hypertext markup language tags from string in SQL Server or the way to dissect hypertext markup language tags and retrieve solely text from string in SQL Server while not exploitation regular expressions or take away text between < and > and acquire solely text from string in SQL Server.

To implement this practicality we'd like to make one user outlined operate to dissect hypertext markup language text and come back solely 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 perform is employed to insert string at given position and delete range of characters given 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're victimisation associate identity column on your SQL Server tables, you'll be able to set following insert price to no matter price you would like.</p>  <p style="margin: 20px 0px; ">It would be wise to first check </p>  ') 

Output :



Previous Post Next Post