如何删除单词之间的空格并仅使用"替换","INSTR"或'SUBSTR' oracle函数替换为单个下划线



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

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

是否可以用"REPLACE"、"INSTR"或"SUBSTR"获得以上输出。

注意:我不想使用replace_regex

嵌套替换可能会有所帮助。
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>

这里的最佳选项可能是使用REGEXP_REPLACE,所以我建议它:

SELECT
col,
REGEXP_REPLACE(col, 's+', '_') AS col_underscore
FROM yourTable;

正则表达式非常适合这里,因为您有未知数量的空白。

最新更新