HI WELCOME TO KANSIRIS

How to Remove HTML Tags from String in SQL Server

Leave a Comment
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>  ')

0 comments:

Post a Comment

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