【问题标题】:How to delete a random substring in oracleoracle中如何删除随机子串
【发布时间】:2014-05-06 04:41:33
【问题描述】:

我需要你帮助解决给定的问题

我有一个列有数据的表

胸部1 [00400 - 00479] 头 1 [00100 - 00228] lab66 [lab661]

the whole date is in single column

我们有一个页面,我们可以在其中发出命令删除任何给定的字符串。

为了删除记录,我们只给出方括号中的代码。

例如,如果我们需要删除 head1 [00100 - 00228],我们只需要 00100 - 00228 来删除字符串。

这里我的问题是写一个查询,我们可以给 00100 - 0022800400 - 00479 删除 head1 [00100 - 00228]thorax1 [00400 - 00479] 记录。

【问题讨论】:

  • 给定数据:thorax1 [00400 - 00479] head1 [00100 - 00228] lab66 [lab661] 和字符串 00100 - 00228。期望的结果字符串是什么?
  • 如果用户试图删除 00100 - 00228 。在这种情况下,期望输出将是 thorax1 [00400 - 00479] lab66 [lab661]。
  • 同样,如果用户尝试删除 00400 - 00479,那么期望输出将是 head1 [00100 - 00228] lab66 [lab661]

标签: oracle oracle10g substring


【解决方案1】:
SQL> with t(col) as (
  2  select 'thorax1 [00400 - 00479] head1 [00100 - 00228] lab66 [lab661]' from dual
  3  )
  4  select regexp_replace(col,'\s{0,1}[[:alnum:]]* \[00100 - 00228\]','') from t
  5  ;

REGEXP_REPLACE(COL,'\S{0,1}[[:ALNUM:]]                                          
--------------------------------------                                          
thorax1 [00400 - 00479] lab66 [lab661]                                          

SQL> with t(col) as (
  2  select 'thorax1 [00400 - 00479] head1 [00100 - 00228] lab66 [lab661]' from dual
  3  )
  4  select regexp_replace(col,'\s{0,1}[[:alnum:]]* \[00400 - 00479\]','') from t
  5  /

REGEXP_REPLACE(COL,'\S{0,1}[[:ALNUM:]                                           
-------------------------------------                                           
 head1 [00100 - 00228] lab66 [lab661]                                           

SQL> with t(col) as (
  2  select 'thorax1 [00400 - 00479] head1 [00100 - 00228] lab66 [lab661]' from dual
  3  )
  4  select regexp_replace(col,'\s{0,1}[[:alnum:]]* \[lab661\]','') from t
  5  /

REGEXP_REPLACE(COL,'\S{0,1}[[:ALNUM:]]*\[LAB6                                   
---------------------------------------------                                   
thorax1 [00400 - 00479] head1 [00100 - 00228]                                   

在 10G 中试试这个(将“COL”列名从分解列表移动到 SELECT 语句):

with t as (
select 'thorax1 [00400 - 00479] head1 [00100 - 00228] lab66 [lab661]' col from dual
)
select regexp_replace(col,'\s{0,1}[[:alnum:]]* \[lab661\]','') from t
/                                                        

【讨论】:

  • SQL> with t(col) as ( select 'thorax1 [00400 - 00479] head1 [00100 - 00228] lab66 [lab661]' from dual ) select regexp_replace(col,'\s{0, 1}[[:alnum:]]* [00100 - 00228]','') 从 t ; with t(col) as ( * ERROR at line 1: ORA-32033: unsupported column aliasing
  • 当我尝试在 oracle 10g 上运行它时出现上述错误。请您帮我解决这个问题。
  • 我添加了应该在上面的 10G 中工作的示例,请参阅。
  • 我认为以下内容可能比正则表达式开头的\s{0,1} 更合乎逻辑:(^|\s)
猜你喜欢
  • 2015-07-14
  • 2013-05-13
  • 2016-12-12
  • 2021-04-12
  • 2018-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多