我在列
中有以下样本值Abc-123-xyz
Def-456-uvw
Ghi-879-rst-123
Jkl-abc
预期输出是由' - '分割的第三个元素,如果没有第三个元素,则将检索最后一个元素。
见下面的预期输出:
Xyz
Uvw
Rst
Abc
非常感谢您的帮助。
答案 0 :(得分:2)
SELECT initcap(nvl(regexp_substr(word, '[^-]+', 1,3),regexp_substr(word, '[^-]+', 1,2))) FROM your_table;
答案 1 :(得分:2)
另一种方法:
SQL> with t1(col) as(
2 select 'Abc-123-xyz' from dual union all
3 select 'Def-456-uvw' from dual union all
4 select 'Ghi-879-rst-123' from dual union all
5 select 'Jkl-Abc' from dual
6 )
7 select regexp_substr( col
8 , '[^-]+'
9 , 1
10 , case
11 when regexp_count(col, '[^-]+') >= 3
12 then 3
13 else regexp_count(col, '[^-]+')
14 end
15 ) as res
16 from t1
17 ;
结果:
RES
---------------
xyz
uvw
rst
Abc
答案 2 :(得分:2)
regexp_substr(column, '(.*?-){0,2}([^-]+)', 1, 1, '', 2)
答案 3 :(得分:0)
您也可以在没有RegEx的情况下执行此操作:
with t1 as(
select 'Abc-123-xyz' as MyText from dual union all
select 'Def-456-uvw' from dual union all
select 'Ghi-879-rst-123' from dual union all
select 'Jkl-Abc' from dual
)
SELECT
SUBSTR(t1.mytext, LENGTH(t1.mytext) - INSTR(REVERSE(t1.mytext), '-') + 2)
FROM t1
;