如何从SQL Server中删除所有的换行符?

时间:2022-05-15 02:50:45

how i can remove all NewLine from a variable in SQL Server?

如何从SQL Server中删除所有的换行符?

I use SQL Server 2008 R2.

我使用SQL Server 2008 R2。

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

我需要在T-Sql命令中删除变量中的所有换行符。

For example :

例如:

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

And it printed like this

它是这样打印的

 12345
        25487
        154814 

But i want get string like this

但是我想要这样的字符串

12345 25487 154814

12345 25487 154814

I write this query but it not work :

我编写这个查询,但它不起作用:

Set @A = Replace(@A,CHAR(13),' ')

2 个解决方案

#1


27  

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;

#2


3  

If you want it to look exactly like in your sample output, use this hack:

如果你想让它看起来和你的样本输出完全一样,使用以下技巧:

DECLARE @A nvarchar(500) 
SET @A = ' 12345
        25487
        154814 '

SET @A = 
  replace(
    replace(
      replace(
        replace(@A, char(13)+char(10),' '),
      ' ','<>'),
    '><','')
  ,'<>',' ')

PRINT @A

It will first replace your newline's then your consecutive spaces with one. Pay attention that it would be wise to url-encode the input string to avoid nasty surprises.

它将首先替换换行符,然后用一个空格替换连续的空格。请注意,最好对输入字符串进行url编码,以避免出现令人不快的意外。

#1


27  

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;

#2


3  

If you want it to look exactly like in your sample output, use this hack:

如果你想让它看起来和你的样本输出完全一样,使用以下技巧:

DECLARE @A nvarchar(500) 
SET @A = ' 12345
        25487
        154814 '

SET @A = 
  replace(
    replace(
      replace(
        replace(@A, char(13)+char(10),' '),
      ' ','<>'),
    '><','')
  ,'<>',' ')

PRINT @A

It will first replace your newline's then your consecutive spaces with one. Pay attention that it would be wise to url-encode the input string to avoid nasty surprises.

它将首先替换换行符,然后用一个空格替换连续的空格。请注意,最好对输入字符串进行url编码,以避免出现令人不快的意外。