SQL Function to remove HTML tags from Varchar field

Following is the function to remove HTML tags from a varchar field in SQL Server


CREATE FUNCTION [dbo].[RemoveHTML]
(
@vHTMLText VARCHAR(MAX)
)
RETURNS VARCHAR(MAX)
AS
BEGIN
    DECLARE @iFirst INT
    DECLARE @iLast INT
    DECLARE @iLength INT
    SET @iFirst = CHARINDEX('<',@vHTMLText)
    SET @iLast = CHARINDEX('>',@vHTMLText,CHARINDEX('<', @vHTMLText))
    SET @iLength = (@iLast - @iFirst) + 1
    WHILE @iFirst > 0 AND @iLast > 0 AND @iLength > 0
    BEGIN
        SET @vHTMLText = STUFF(@vHTMLText,@iFirst,@iLength,'')
        SET @iFirst =CHARINDEX('<',@vHTMLText)
        SET @iLast =CHARINDEX('>' ,@vHTMLText, CHARINDEX('<', @vHTMLText))
        SET @iLength = (@iLast - @iFirst) + 1
    END
    RETURN LTRIM(RTRIM(@vHTMLText))
END

Gopikrishna

    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment