Removing embedded HTML tags with SQL

This is an interesting piece of sql, the function accepts a string and removes HTML elements that is inside the string. This is genereally useful when you have data that has embedded HTML content on it. via

Create Function dbo.UTILfn_StripTags
(@Dirty varchar(4000))
Returns varchar(4000)
As
Begin
Declare @Start int,
@End int,
@Length int
While CharIndex('<', @Dirty) > 0 And CharIndex('>', @Dirty, CharIndex('<', @Dirty)) > 0
Begin
Select @Start = CharIndex('<', @Dirty),
@End = CharIndex('>', @Dirty, CharIndex('<', @Dirty))
Select @Length = (@End - @Start) + 1
If @Length > 0
Begin
Select @Dirty = Stuff(@Dirty, @Start, @Length, '')
End
End
return @Dirty
End

Leave a comment