Skip to content

Instantly share code, notes, and snippets.

@Otterpohl
Created May 13, 2022 16:04
Show Gist options
  • Save Otterpohl/4204d4471f6d28f629957ebe10448351 to your computer and use it in GitHub Desktop.
Save Otterpohl/4204d4471f6d28f629957ebe10448351 to your computer and use it in GitHub Desktop.
Function to strip html tags from data
CREATE FUNCTION [dbo].[fn_StripHTML] (@HTMLText VARCHAR(MAX))
RETURNS VARCHAR(MAX) AS
BEGIN
DECLARE @Start INT = CHARINDEX('<',@HTMLText)
DECLARE @End INT = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))
DECLARE @Length INT = (@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment