Oracle PL/SQL : remove “space characters” from a string

后端 未结 6 1844
迷失自我
迷失自我 2021-02-02 10:48

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

6条回答
  •  粉色の甜心
    2021-02-02 11:46

    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
    

提交回复
热议问题