I have a user-defined function in Microsoft SQL Server, and I have a problem getting it to work correctly.
The function takes in one NVARCHAR(MAX) parameter that will then strip all the HTML tags from that string text and then remove all the extra white spaces via a cursor
CREATE FUNCTION UDF_STRIP_HTML
(@HTMLText NVARCHAR(MAX))
RETURNS NVARCHAR(MAX) AS
BEGIN
DECLARE @Start INT
DECLARE @End INT
DECLARE @Length INT
SET @Start = CHARINDEX('<',@HTMLText)
SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@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('<', @HTMLText)
SET @End = CHARINDEX('>', @HTMLText, CHARINDEX('<', @HTMLText))
SET @Length = (@End - @Start) + 1
END
DECLARE @WORD VARCHAR(MAX)
DECLARE @STRING VARCHAR(MAX)
DECLARE AUDIT_TRAIL_CURSOR CURSOR
FOR
--Split string into individual words and ignore blanks or extra spaces
SELECT VALUE
FROM STRING_SPLIT(@HTMLText, ' ')
WHERE LTRIM(RTRIM(VALUE)) <> ''
OPEN AUDIT_TRAIL_CURSOR
FETCH NEXT FROM AUDIT_TRAIL_CURSOR INTO @WORD
WHILE @@FETCH_STATUS = 0
BEGIN
--Strip extra spaces from each word and add only one space after
SET @STRING = CONCAT(@STRING, RTRIM(LTRIM(@WORD)), ' ')
FETCH NEXT FROM AUDIT_TRAIL_CURSOR INTO @WORD
END
CLOSE AUDIT_TRAIL_CURSOR
DEALLOCATE AUDIT_TRAIL_CURSOR
RETURN @STRING
END
GO
The HTML stripper works fine either way, but the whitespace stripper only works if I directly pass in a hard coded string like in
This works:
SELECT dbo.UDF_STRIP_HTML('Some <b/> string with Words. ')
HTML stripper works, but whitespace stripper does not:
SELECT dbo.UDF_STRIP_HTML(some_column)
FROM some_table
NOTE: I understand this may not be the best function, but this will be for a one time data export via query