Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can remove all NewLine from a variable in SQL Server?

How can I can remove all NewLine from a variable in SQL Server?

I use SQL Server 2008 R2.

I need remove all NewLine in a variable in a T-Sql Command.

For example:

Declare @A NVarChar(500) 
Set @A = ' 12345
        25487
        154814 '
Print @A

And it printed like this:

 12345
        25487
        154814 

But I want to get strings like this:

12345 25487 154814

I write this query, but it does not work:

Set @A = Replace(@A,CHAR(13),' ')
like image 901
Negar Jafarpisheh Avatar asked Aug 06 '13 06:08

Negar Jafarpisheh


People also ask

How do I remove a line break in SQL query?

Remove and Replace Carriage Returns and Line Breaks in SQL Using SQL to remove a line feed or carriage return means using the CHAR function. A line feed is CHAR(10); a carriage return is CHAR(13).

How do you remove carriage return and line feed in SQL?

Char(13) is the carriage return and char(10) is the line feed symbol. How to remove line feed from a SQL Server column ? To remove the carriage return or line feed directly from a SQL column, simply apply a replace method to remove it and replace with a space or an empty string.

How do you handle a new line character in SQL?

-- Using both \r\n SELECT 'First line. \r\nSecond Line. ' AS 'New Line'; -- Using both \n SELECT 'First line.


1 Answers

You must use this query

Declare @A NVarChar(500);

Set @A = N' 12345
        25487
        154814 ';

Set @A = Replace(@A,CHAR(13)+CHAR(10),' ');

Print @A;
like image 74
Ardalan Shahgholi Avatar answered Oct 07 '22 23:10

Ardalan Shahgholi