Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle REPLACE() function isn't handling carriage-returns & line-feeds

Tags:

sql

oracle

We've a table with a varchar2(100) column, that occasionally contains carriage-return & line-feeds. We should like to remove those characters in the SQL query. We're using:

REPLACE( col_name, CHR(10) )  

which has no effect, however replacing 'CHR(10)' for a more conventional 'letter' character proves that the REPLACE function works otherwise. We have also found that

REPLACE( col_name, CHR(10), '_' )  

finds the location of the new-line, but inserts the underscore after it, rather than replacing it.

Running on Oracle8i. Upgrading is not an option.

like image 801
Martin Cowie Avatar asked Jan 02 '09 15:01

Martin Cowie


People also ask

How do I replace a carriage return in Oracle?

For carriage return ascii value is 13. Use CHR(13) for carraige return.

How do I replace a carriage return in SQL?

In the Find box hold down the Alt key and type 0 1 0 for the line feed and Alt 0 1 3 for the carriage return. They can now be replaced with whatever you want.

How can I replace multiple characters in a string in Oracle?

The Oracle/PLSQL REGEXP_REPLACE function is an extension of the REPLACE function. This function, introduced in Oracle 10g, will allow you to replace a sequence of characters in a string with another set of characters using regular expression pattern matching.


1 Answers

Another way is to use TRANSLATE:

TRANSLATE (col_name, 'x'||CHR(10)||CHR(13), 'x') 

The 'x' is any character that you don't want translated to null, because TRANSLATE doesn't work right if the 3rd parameter is null.

like image 197
Tony Andrews Avatar answered Nov 08 '22 21:11

Tony Andrews