2011-03-31 70 views
19

在我的Oracle 10g数据库,我想从表中字段的值删除“空白字符”(空格,制表符,回车......)删除“空格字符”。的Oracle PL/SQL:从一个字符串

TRANSLATE()要走的路?例如像:

MY_VALUE := TRANSLATE(MY_VALUE, 
    CHR(9) || CHR(10) || CHR(11) || CHR(12) || CHR(13) || ' ', ''); 

或者是还有什么更好的选择(像[:space:]在PHP PCRE)?

感谢您的任何忠告。

+2

顺便说一句,你的'TRANSLATE'将不起作用,因为你有NULL作为第三个参数。你可以使用'TRANSLATE(my_value,'A'|| CHR(9)|| CHR(10)|| CHR(11)|| CHR(12)|| CHR(13)||'','A') ' – 2011-04-01 06:12:19

+1

你是对的,谢谢!哦,我爱甲骨文... :-P – 2011-04-01 08:28:16

+0

删除在甲骨文的PL/SQL任何空格和表单 – 2016-05-23 13:04:00

回答

36

我会去REGEXP_REPLACE,虽然我不是100%肯定这是PL/SQL

my_value := regexp_replace(my_value, '[[:space:]]*',''); 
+0

我来试试,明天的工作,并会随时向你通报。由于已经为指向'REGEXP_REPLACE'(http://download.oracle.com/docs/cd/B19306_01/server。102/b14200/functions130.htm)。看起来这就是我所需要的,因为它声称是“POSIX”兼容的,所以'[[:space:]]'应该被识别。 – 2011-03-31 20:34:36

+0

我确认这是有效的。 – 2011-04-01 08:29:28

2
select regexp_replace('This is a test ' || chr(9) || ' foo ', '[[:space:]]', '') from dual; 

REGEXP_REPLACE 
-------------- 
Thisisatestfoo 
6

既然你是舒适的使用正则表达式使用,您可能需要使用REGEXP_REPLACE函数。 [:空间:]如果你想消除任何比赛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| 

如果你想留在原地一个空间,每一套连续的空格字符,只需添加+正则表达式和使用作为替代人物的空间。

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

要删除任何空格,您可以使用:

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

举例:删除所有空格中的表:

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

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

这是更高性能还是更好比这里已经提出的基于正则表达式的解决方案? – 2012-11-07 11:11:53

11

较短的版本中:

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

是:

REGEXP_REPLACE(my_value, '\s') 

无论上述声明将消除 “空” 字。

要删除“空”包住用如下语句替代

像这样:

REPLACE(REGEXP_REPLACE(my_value, '\s'), CHR(0)) 
+0

感谢您的NULL删除说明。真的需要这个东西。 – Reimius 2017-04-26 15:52:14

-1

由单个空格替换一个或多个空格,你应该使用{2,}代替*,否则你会insert空白的所有非空白字符之间。

REGEXP_REPLACE(my_value, '[[:space:]]{2,}', ' ') 
相关问题