问题
how 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 get string like this
12345 25487 154814
I write this query but it not work :
Set @A = Replace(@A,CHAR(13),' ')
回答1:
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:
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.
回答3:
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!
来源:https://stackoverflow.com/questions/18073190/how-i-can-remove-all-newline-from-a-variable-in-sql-server