Files
Fuchs_Intranet/Fuchs_Database/dbo/Functions/strings_encodeHTML.sql
T
StefanandCopilot 330c33e65d
Playwright Tests / test (push) Waiting to run
Move fis widgets SQL into scalar functions
Add fis_wdg_* table functions for dashboard widgets and wire them into
FuchsDatabase.sqlproj; update the invoice list procs accordingly.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-16 11:35:28 +02:00

26 lines
1.1 KiB
Transact-SQL

CREATE FUNCTION [dbo].[strings_encodeHTML](@inputText NVARCHAR(MAX))
RETURNS NVARCHAR(MAX)
AS
BEGIN
DECLARE @encodedText NVARCHAR(MAX) = N'';
IF DATALENGTH(@inputText) > 0
BEGIN
/* 1) Escape reserved HTML characters (& < >); FOR XML also entitizes CR as &#x0D; */
SET @encodedText = CAST((SELECT @inputText AS [p] FOR XML PATH(''), TYPE) AS NVARCHAR(MAX));
/* 2) Remove the leading <p> and trailing </p> that FOR XML adds */
SET @encodedText = SUBSTRING(@encodedText, 4, LEN(@encodedText) - 7);
/* 3) Line breaks (CRLF, LF, lone CR) -> <br>; NBSP -> &nbsp; */
SET @encodedText = REPLACE(@encodedText, N'&#x0D;' + NCHAR(10), N'<br>');
SET @encodedText = REPLACE(@encodedText, NCHAR(13) + NCHAR(10), N'<br>');
SET @encodedText = REPLACE(@encodedText, NCHAR(10), N'<br>');
SET @encodedText = REPLACE(@encodedText, N'&#x0D;', N'<br>');
SET @encodedText = REPLACE(@encodedText, NCHAR(13), N'<br>');
SET @encodedText = REPLACE(@encodedText, NCHAR(160), N'&nbsp;');
END
RETURN @encodedText;
END;