Menu

Want to work with us? We're on the lookout for digital experts.

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.

This site uses essential cookies for parts of the site to operate and have already been set. Find out more about how we use cookies and how you may delete them. You may delete cookies, but parts of the site will not work.