How can I remove \ n (Char (10)) from a specific line from the beginning and end of a line in ms sql

I have one column for comments, and I need to show this for one report. Here, what happens for a while, users use several inputs in the comment field. I can’t access the code part, I need to manage this thing only in SQL.

So, I removed the unwanted

1 /r/n 
2 /n/n

using

REPLACE(REPLACE(Desc, CHAR(13)+CHAR(10), CHAR(10)),CHAR(10)+CHAR(10), CHAR(10)) as Desc,

Now I want to remove any from \reither \nthe beginning or ending line, if any

+4
source share
3 answers

, : ( char (10) char (13) )
. , (Ctrl + T).

Results to Ttext:

Results to Grid:

+2

TRIM check

: UPDATE tablename SET descriptions = TRIM(TRAILING "<br>" FROM descriptions) , -

SELECT REPLACE(REPLACE(@str, CHAR(13), ''), CHAR(10), '')

DECLARE @testString varchar(255)
set @testString = 'MY STRING      '

 /*Select the string and try to copy and paste into notepad and tab is still there*/
SELECT testString = @testString 

/*Ok, it seems easy, let try to trim this. Huh, it doesn't work, the same result here.*/
SELECT testStringTrim = RTRIM(@testString) 

/*Let try to get the size*/
SELECT LenOfTestString = LEN(@testString) 

/*This supposed to give us string together with blank space, but not for tab though*/
SELECT DataLengthOfString= DATALENGTH(@testString)

SELECT ASCIIOfTab = ASCII(' ')
SELECT CHAR(9) 

/*I always use this like a final solution*/
SET @testString = REPLACE(REPLACE(REPLACE(@testString, CHAR(9), ''), CHAR(10), ''), CHAR(13), '') SELECT @testString   

/*
CHAR(9)       - Tab
CHAR(10) - New Line
CHAR(13) - Carriage Return
*/

+1
select dbo.trim('abc','c') -- ab
select dbo.trim('abc','a') -- bc
select dbo.trim(' b ',' ') -- b

enter image description here
user-define: trim()

: , \r,\n ..
Create FUNCTION Trim
(
    @Original varchar(max), @letter char(1)
)
RETURNS varchar(max)
AS
BEGIN
    DECLARE @rtrim varchar(max)
    SELECT @rtrim = iif(right(@original, 1) = @letter, left(@original,datalength(@original)-1), @original)
    return iif( left(@rtrim,1) =  @letter, right(@rtrim,datalength(@rtrim)-1),@rtrim)
END
0

Source: https://habr.com/ru/post/1569954/


All Articles