tags:

views:

2875

answers:

4

I would like to replace (or remove) a newline character in a TSQL-string. Any Ideas?

(UPDATE) The obvious

REPLACE(@string,CHAR(13),'')

just won't do it...

+11  A: 
REPLACE(@string, CHAR(13)+CHAR(10), '')
Mitch Wheat
Is chr working? isn't it char?
Peter
ah, sorry typo!
Mitch Wheat
+1  A: 

Did you consider the REPLACE function?

John Saunders
sure, i got stuck on char(13) forgetting the CR at first, then asked the question because i thought it should be in SO
Peter
+6  A: 

The Newline in T-SQL is represented by CHAR(13) & CHAR(10) (Carriage return + Line Feed). Accordingly, you can create a REPLACE statement with the text you want to replace the newline with.

REPLACE(MyField, CHAR(13) + CHAR(10), 'something else')
Cerebrus
+ 1, i accepted Mitch Wheat cause he came up with it first (event though the chr function doesnt exist in my tsql)
Peter
Hey, no problem! Mitch rocks as usual. Few guys can be as fast as him. ;-)
Cerebrus
@Cerebrus: not sure that's true! ;) (voted you up as yours was correct)
Mitch Wheat
+2  A: 

Actually a new line in a SQL command or script string can be any of CR, LF or CR+LF. To get them all, you need something like this:

SELECT REPLACE(REPLACE(@str, CHAR(13), ''), CHAR(11), '')
RBarryYoung