Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle PL/SQL : remove "space characters" from a string

Tags:

In my Oracle 10g database I would like to remove "space characters" (spaces, tabs, carriage returns...) from the values of a table field.

Is TRANSLATE() the way to go ? For example something like:

MY_VALUE := TRANSLATE(MY_VALUE,   CHR(9) || CHR(10) || CHR(11) || CHR(12) || CHR(13) || ' ', ''); 

Or is there any better alternative (something like [:space:] in PHP PCRE) ?

Thanks for any piece of advice.

like image 580
Frosty Z Avatar asked Mar 31 '11 20:03

Frosty Z


People also ask

How do I trim a space between strings in Oracle?

Answers. Use TRIM, LTRIM, RTRIM and REPLACE (if needed).

How do you remove spaces and special characters from a string in Oracle?

The Oracle REGEXP_REPLACE() function replaces a sequence of characters that matches a regular expression pattern with another string. The REGEXP_REPLACE() function is an advanced version of the REPLACE() function.


2 Answers

I'd go for regexp_replace, although I'm not 100% sure this is usable in PL/SQL

my_value := regexp_replace(my_value, '[[:space:]]*','');  
like image 153
a_horse_with_no_name Avatar answered Oct 23 '22 10:10

a_horse_with_no_name


Shorter version of:

REGEXP_REPLACE( my_value, '[[:space:]]', '' ) 

Would be:

REGEXP_REPLACE( my_value, '\s') 

Neither of the above statements will remove "null" characters.

To remove "nulls" encase the statement with a replace

Like so:

REPLACE(REGEXP_REPLACE( my_value, '\s'), CHR(0)) 
like image 31
Lord Gordoff Avatar answered Oct 23 '22 11:10

Lord Gordoff