Menu

Want to work with us? We're on the lookout for a UX/UI Designer and Digital Project Manager.

We're hiring

Function to strip specific HTML tags in SQL

Web Bureau


21 August 2012 by Web Bureau

Simple modification of the Strip HTML database function allows you to strip out specific HTML tags.  In this example I stripped out the <embed> tag.

 

CREATE FUNCTION [dbo].[udf_StripHTMLEmbedTag]
(@HTMLText VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @Start INT
DECLARE @End INT
DECLARE @Length INT
SET @Start = CHARINDEX('<embed',@HTMLText)
SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<embed',@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('<embed',@HTMLText)
SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<embed',@HTMLText))
SET @Length = (@End - @Start) + 1
END
RETURN LTRIM(RTRIM(@HTMLText))
END

 

Update your database:

 

UPDATE tbl_Products SET description=dbo.udf_StripHTMLEmbedTag(description)

Grow your businessStart a project with us today.