:SetVar Function "AUX:ToSingleLine"
:r $(DropFunctionScript_AUX)
create function [$(Function)]( @Text $(dt_String_AUX) )
returns $(dt_String_AUX)
BEGIN
/*==================================================================================================
# SQLAUX scripting library | Author: Sergei Y.Kitáev
----------------------------------------------------------------------------------------------------
#
# TEXT TRANSFORMATION (OF STRINGS, CONCATENATED THROUGH CR+LF) TO SINGLE LINE (WITHOUT LINE FEEDS):
#
# === Usage: ===
# dbo.[AUX:ToSingleLine]( <String with CR-n-LFs> )
#
==================================================================================================*/

if @Text is null return null

set @Text = TRIM( @Text )

declare @Result $(dt_String_AUX), @String $(dt_String_AUX), @L int, @P int, @Ch nchar, @CRLF bit, @PreCRLF bit
select @Result = '', @String = '', @L = LEN( @Text ), @P = 1
while @P <= @L
begin
   set @Ch = SUBSTRING( @Text, @P, 1 )
   set @CRLF = case
                  when @Ch in ( $(CR_LF_AUX) )
                  then 1 else 0
               end
   if @P = @L or @CRLF = 1
   begin
      if @P = @L or @PreCRLF <> 1
      begin
         if @P = @L and @CRLF = 0
            set @String = @String + @Ch
         set @String = TRIM( @String )
         if @String <> ''
            set @Result = @Result +
               case
                  when @Result<>''
                  then ' ' else ''
               end + @String
      end
      set @String = ''
   end
   else
      set @String = @String + @Ch
   select @PreCRLF = @CRLF, @P = @P + 1
end

return @Result

/*================================================================================================*/
END
GO
/*
select '“'+dbo.[AUX:ToSingleLine](
N'  String-1.  '+CHAR(13)+CHAR(10)+
N'String-2.  '+CHAR(13)+
N'  String-3.  '+CHAR(10)
)+'”' 'String without CRLFs'
*/
