简体   繁体   English

如何在Oracle SQL中的第一个连字符之后和第三个连字符之前提取数据

[英]How to extract data AFTER 1st hyphen and before 3rd hyphen in Oracle SQL

I have a requirement, Where i have a column value like STEP_D1_DEVTS_MN_PQ_LS 我有一个要求,我有一个像STEP_D1_DEVTS_MN_PQ_LS这样的列值

I want the value after 1st underscore and before 3rd underscore and then get the replace the underscore with hypen like below 我想要第一个下划线之后和第三个下划线之前的值,然后用如下所示的hypen替换下划线

D1-DEVTS . D1-DEVTS

Can some one please help me out here ? 有人可以帮我吗?

I tried SUBSTR and INSTR that did not help me out. 我尝试了对我没有帮助的SUBSTRINSTR

Another is to use SUBSTR + INSTR (along with REPLACE , of course): 另一种方法是使用SUBSTR + INSTR (当然还有REPLACE ):

SQL> with test (col) as
  2    (select 'STEP_D1_DEVTS_MN_PQ_LS' from dual)
  3  select
  4    replace(substr(col,
  5                   instr(col, '_', 1, 1) + 1,                          --> start after the 1st underscore
  6                   instr(col, '_', 1, 3) - instr(col, '_', 1, 1) - 1   --> take everything that's between 1st and 3rd underscore
  7                  ), '_', '-') result                                  --> replace _ with -
  8  from test;

RESULT
--------------------
D1-DEVTS

SQL>

One option would be using concurrent regexp_substr expressions with a replace 一种选择是使用带有replace并发regexp_substr表达式

with t(str) as
(
 select 'STEP_D1_DEVTS_MN_PQ_LS' from dual   
)    
select replace(regexp_substr(str,'[^_]+[_]',1,2)||regexp_substr(str,'[^_]+',1,3),'_','-') 
    as "Result String"
  from t;

Result String
-------------
D1-DEVTS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM