TrimEnters

SQL Function to remove repeated Enters from a string in SQL
Chr(13) + Chr(10)


Public

Tested

My Own Work
// SQL function to trim enters (up to 6 enters at start of string)
// To use
// select TrimEnters(StrValue) from thatTable
CREATE FUNCTION dbo.TrimEnters(@string NVARCHAR(MAX))
RETURNS NVARCHAR(MAX)
BEGIN
declare @Ent1 as varchar(4) = CHAR(13) + CHAR(10)
declare @str54 as nvarchar(max), $str55 as nvarchar(max)
set @str54 = Replace(@string, @Ent1 + @Ent1 + @Ent1 + @Ent1 + @Ent1 + @Ent1, @Ent1)     /* 6 Enters */
set @str54 = Replace(@str54, @Ent1 + @Ent1 + @Ent1 + @Ent1 + @Ent1, @Ent1)             /* 5 Enters */
set @str54 = Replace(@str54, @Ent1 + @Ent1 + @Ent1 + @Ent1, @Ent1)                     /* 4 Enters */
set @str54 = Replace(@str54, @Ent1 + @Ent1 + @Ent1, @Ent1)                         /* 3 Enters */
set @str54 = Replace(@str54, @Ent1 + @Ent1, @Ent1)                                 /* 2 Enters */
SELECT @str55 = CASE WHEN @str54 LIKE @Ent1 + '%'                                /* Enter found at start of string */
    THEN Right(@str54, 3)
    ELSE @str54
END;
RETURN LTRIM(RTRIM(@str54))
END
GO

StrValue

select TrimEnters(column) from thatTable

Views 386 Downloads 153

SQL Texts + Strings
ANmarAmdeen
791
Attachments
Revisions

v1.0