﻿
CREATE FUNCTION [dbo].[strings_encodeHTML](@inputText NVARCHAR(MAX))
RETURNS NVARCHAR(MAX)
AS
BEGIN
    DECLARE @encodedText NVARCHAR(MAX);

    -- Use FOR XML PATH to escape special characters
    SET @encodedText = CAST((SELECT @inputText AS [p] FOR XML PATH(''), TYPE) as nvarchar(max));

    RETURN IIF(LEN(@encodedText) < 7, '', dbo.strings_replaceSpecialCharsInHTML(SUBSTRING(@encodedText, 4, LEN(@encodedText) -7)));
END;