首页 > 解决方案 > 如何仅使用“REPLACE”、“INSTR”或“SUBSTR”oracle函数删除单词之间的空格并用单个下划线替换

问题描述

我想使用 'REPLACE'、'INSTR' 或 'SUBSTR' oracle 函数用单个下划线替换单词之间的空格。

example:

    "my name is            xyz" =>  "my_name_is_xyz"
    "test    sdf"               =>  "test_sdf"

是否可以使用 'REPLACE','INSTR' OR 'SUBSTR' 获得高于输出。

注意:我不想使用 replace_regex

标签: oracle

解决方案


嵌套替换可能会有所帮助。

SQL> with test (col) as
  2    (select 'my name is            xyz' from dual union all
  3     select 'test    sdf'               from dual
  4    )
  5  select col,
  6    replace(replace(replace(replace(col, ' ', '# '), ' #'), '#'), ' ', '_') result
  7  from test;

COL                       RESULT
------------------------- --------------------------------------------------
my name is            xyz my_name_is_xyz
test    sdf               test_sdf

SQL>

推荐阅读