sql Strip HTML Function

CREATE FUNCTION [dbo].[udf_StripHTML](
	@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

SELECT dbo.udf_StripHTML('<p>This is some HTML</p>')
Function to strip HTML from a string.

Updated: Friday 8th October 2010, 14:51pm

There are 0 comments

Leave a comment of your own

Comments are currently closed.