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

血红的双手。 提交于 2019-12-01 03:03:25
Ardalan Shahgholi

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;
OzrenTkalcecKrznaric

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.

Replace(@A,CHAR(13)+CHAR(10),' ') didn't remove all the spaces for me.

Instead I used

replace(replace(@A, char(13),N' '),char(10),N' ')

This works well!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!