Fro*_*y Z 21 regex oracle plsql
在我的Oracle 10g数据库中,我想从表字段的值中删除"空格字符"(空格,制表符,回车符...).
是TRANSLATE()要走的路?例如:
MY_VALUE := TRANSLATE(MY_VALUE,
CHR(9) || CHR(10) || CHR(11) || CHR(12) || CHR(13) || ' ', '');
Run Code Online (Sandbox Code Playgroud)
或者有更好的选择(类似于[:space:]PHP PCRE)?
感谢您的任何建议.
a_h*_*ame 38
我会去regexp_replace,虽然我不是100%肯定这在PL/SQL中是可用的
my_value := regexp_replace(my_value, '[[:space:]]*','');
Run Code Online (Sandbox Code Playgroud)
小智 13
更短版本:
REGEXP_REPLACE( my_value, '[[:space:]]', '' )
Run Code Online (Sandbox Code Playgroud)
将会:
REGEXP_REPLACE( my_value, '\s')
Run Code Online (Sandbox Code Playgroud)
以上两个语句都不会删除"null"字符.
要删除"nulls",请使用replace包含该语句
像这样:
REPLACE(REGEXP_REPLACE( my_value, '\s'), CHR(0))
Run Code Online (Sandbox Code Playgroud)
由于您对正则表达式感到满意,因此您可能希望使用REGEXP_REPLACE函数.如果你想消除与[:space:] POSIX类相匹配的任何东西
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|
Run Code Online (Sandbox Code Playgroud)
如果要为每组连续空格字符留出一个空格,只需将其添加+到正则表达式并使用空格作为替换字符.
with x as (
select 'abc 123 234 5' str
from dual
)
select regexp_replace( str, '[[:space:]]+', ' ' )
from x
Run Code Online (Sandbox Code Playgroud)