在Oracle SQL中删除in string中的空格

时间:2017-02-01 11:44:58

标签: sql oracle

嗨我需要删除字符串中的空格。可以任何人帮我这些。

String1 = "Biotin-dPEG <REFERENCE ID="9869" TYPE="trademark"/>n-azide products are single molecular weight dPEG <REFERENCE ID="9869" TYPE="trademark"/> products that permit biotin labeling with precise spacing" .

我需要删除string1中<REFRENCE ID = "9869 ...>之前的空格。

3 个答案:

答案 0 :(得分:2)

使用regexp_replace

with t(col) as (
  select 'Biotin-dPEG <REFERENCE ID="9869" TYPE="trademark"' from dual
)
-- test data. Dont mind the above --

select
  regexp_replace(col, '\s+(<REFERENCE)','\1')
from t;

产地:

Biotin-dPEG<REFERENCE ID="9869" TYPE="trademark"

答案 1 :(得分:0)

这将删除=

周围的所有空格
select  regexp_replace('String1 = "Biotin-dPEG <REFERENCE ID="9869" TYPE="trademark"/>n-azide products are single molecular weight dPEG <REFERENCE ID = "9869" TYPE="trademark"/> products that permit biotin labeling with precise spacing" .
','\s*=\s*','=')
from    dual
String1="Biotin-dPEG <REFERENCE ID="9869" TYPE="trademark"/>n-azide products are single molecular weight dPEG <REFERENCE ID="9869" TYPE="trademark"/> products that permit biotin labeling with precise spacing" .

这是一个简单的演示

select  regexp_replace('A =  1,B=   2,C=3,D=  4,E   =  5','\s*=\s*','=')
from    dual
A=1,B=2,C=3,D=4,E=5

https://docs.oracle.com/cd/E18283_01/olap.112/e17122/dml_functions_2068.htm

https://docs.oracle.com/cd/E18283_01/server.112/e17118/ap_posix.htm#SQLRF020

答案 2 :(得分:0)

Oracle安装程序

CREATE TABLE data ( string ) AS
SELECT 'Biotin-dPEG <REFERENCE ID="9869" TYPE="trademark"/>' FROM DUAL UNION ALL
SELECT 'Biotin-dPEG <REFERENCE TYPE="trademark" ID="9869"/>' FROM DUAL;

<强>查询

SELECT REGEXP_REPLACE(
         UPDATEXML(
           xml,
           '/root/REFERENCE[@ID="9869"][1]/preceding-sibling::text()',
           RTRIM(
             EXTRACT(
               xml,
               '/root/REFERENCE[@ID="9869"][1]/preceding-sibling::text()'
             )
           )
         ).getClobVal(),
         '^<root>|</root>$|^<root/>$'
       ) AS string
FROM   ( SELECT XMLType( '<root>' || string || '</root>' ) AS xml
         FROM   DUAL );

<强>输出

STRING
--------------------------------------------------
Biotin-dPEG<REFERENCE ID="9869" TYPE="trademark"/>
Biotin-dPEG<REFERENCE TYPE="trademark" ID="9869"/>