24

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.

3
  • 2
    By the way, your TRANSLATE won't work because you have NULL as the third parameter. You could use TRANSLATE(my_value,'A'||CHR(9)||CHR(10)||CHR(11)||CHR(12)||CHR(13)||'','A') Commented Apr 1, 2011 at 6:12
  • 1
    You're right, thanks! Oh I love Oracle... :-p Commented Apr 1, 2011 at 8:28
  • Remove any whitespaces in Oracle PL/SQL and Forms Commented May 23, 2016 at 13:04

6 Answers 6

48

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:]]*',''); 
Sign up to request clarification or add additional context in comments.

1 Comment

I'll try it tomorrow at work, and will keep you informed. Thanks already for pointing REGEXP_REPLACE (download.oracle.com/docs/cd/B19306_01/server.102/b14200/…). Seems that's what I need since it claims to be "POSIX" compatible so [[:space:]] should be recognized.
23

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))

1 Comment

Thanks for the NULL removal explanation. Really needed this for something.
8

Since you're comfortable with regular expressions, you probably want to use the REGEXP_REPLACE function. If you want to eliminate anything that matches the [:space:] POSIX class

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


SQL> ed
Wrote file afiedt.buf

  1  select '|' ||
  2         regexp_replace( 'foo ' || chr(9), '[[:space:]]', '' ) ||
  3         '|'
  4*   from dual
SQL> /

'|'||
-----
|foo|

If you want to leave one space in place for every set of continuous space characters, just add the + to the regular expression and use a space as the replacement character.

with x as (
  select 'abc 123  234     5' str
    from dual
)
select regexp_replace( str, '[[:space:]]+', ' ' )
  from x

1 Comment

Could this regex be improved to also remove a leading space? I had to wrap this in a trim to accommodate the leading.
4
select regexp_replace('This is a test   ' || chr(9) || ' foo ', '[[:space:]]', '') from dual;

REGEXP_REPLACE
--------------
Thisisatestfoo

Comments

2

To replace one or more white space characters by a single blank you should use {2,} instead of *, otherwise you would insert a blank between all non-blank characters.

REGEXP_REPLACE( my_value, '[[:space:]]{2,}', ' ' )

Comments

0

To remove any whitespaces you could use:

myValue := replace(replace(replace(replace(replace(replace(myValue, chr(32)), chr(9)),  chr(10)), chr(11)), chr(12)), chr(13));

Example: remove all whitespaces in a table:

update myTable t
    set t.myValue = replace(replace(replace(replace(replace(replace(t.myValue, chr(32)), chr(9)), chr(10)), chr(11)), chr(12)), chr(13))
where
    length(t.myValue) > length(replace(replace(replace(replace(replace(replace(t.myValue, chr(32)), chr(9)), chr(10)), chr(11)), chr(12)), chr(13)));

or

update myTable t
    set t.myValue = replace(replace(replace(replace(replace(replace(t.myValue, chr(32)), chr(9)), chr(10)), chr(11)), chr(12)), chr(13))
where
    t.myValue like '% %'

1 Comment

Is this more performant or otherwise better than the regular expression based solutions already proposed here?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.