SQL – Remove HTML – Parse HTML – User Defined Function

Sample data

Solution

Step 1: Create user defined funtion

CREATE FUNCTION [removeHTML] (@HTMLText VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @Start INT
DECLARE @End INT
DECLARE @Length INT
SET @Start = CHARINDEX(‘<‘,@HTMLText) SET @End = CHARINDEX(‘>’,@HTMLText,CHARINDEX(‘<‘,@HTMLText)) SET @Length = (@End – @Start) + 1 WHILE @Start > 0
AND @End > 0
AND @Length > 0
BEGIN
SET @HTMLText = STUFF(@HTMLText,@Start,@Length,”)
SET @Start = CHARINDEX(‘<‘,@HTMLText) SET @End = CHARINDEX(‘>’,@HTMLText,CHARINDEX(‘<‘,@HTMLText))
SET @Length = (@End – @Start) + 1
END
RETURN LTRIM(RTRIM(@HTMLText))
END
GO

Step 2: Use the function as below query

select dbo.removeHTML(Description) as Description from tblname