Thursday, 31 July 2014

Removing HTML tags from Input String in Sql Server

CREATE FUNCTION [dbo].[FN_AMMU_REMOVE_HTML_TAGS]
(
@INPUT_STRING NVARCHAR(MAX)
)
   
RETURNS NVARCHAR(MAX)
AS
BEGIN
    DECLARE @Start INT, @End INT, @Length INT;
   
    WHILE CHARINDEX('<', @INPUT_STRING) > 0 AND CHARINDEX('>', @INPUT_STRING, CHARINDEX('<', @INPUT_STRING)) > 0
    BEGIN
        SET @Start  = CHARINDEX('<', @INPUT_STRING);
        SET @End    = CHARINDEX('>', @INPUT_STRING, CHARINDEX('<', @INPUT_STRING));
        SET @Length = (@End - @Start) + 1;
       
        IF @Length > 0
        BEGIN
            SET @INPUT_STRING = STUFF(@INPUT_STRING, @Start, @Length, '')
        END
     END
   
    RETURN RTRIM(@INPUT_STRING);
END 

No comments :

Post a Comment