Strip HTML Tags – MS SQL
Here is a UDF (user defined function) to strip off html tags from a string in MS SQL.
CREATE FUNCTION [dbo].[StripHtmlTags]
(
– Add the parameters for the function here
@HTMLText NVARCHAR(MAX)
)
RETURNS NVARCHAR(MAX)
AS
BEGINDECLARE @Start INT
DECLARE @End INT
DECLARE @Length INTSET @Start = CHARINDEX(‘<’,@HTMLText)
SET @End = CHARINDEX(‘>’,@HTMLText,CHARINDEX(‘<’,@HTMLText))
SET @Length = (@End – @Start) + 1WHILE @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
ENDRETURN LTRIM(RTRIM(@HTMLText))
END
Example Use:
Execute StripHtmlTags('my <b>html filled</b> string');
Note: The use of MAX is allowed on MS SQL 2005 or newer versions of MS SQL servers. To use it with MS SQL versions older then 2005 (e.g. 2000) replace the MAX with a number for example 1500.










Leave your response!