使用子字符串和instring提取字符串

时间:2015-10-16 17:40:23

标签: oracle-sqldeveloper plsqldeveloper

我有一个问题,我使用了许多方法,如instring和substring来获取输出,但是遗漏了一些东西 以下是数据

V V1

4栏:longrun:runner:rod:robi

4栏:longrun:runner:rod:robi

3 longrun:jog:rod:robi

2 longrun:sprint:runner:rod:robi

我想输出

V V1

4 longrun

4 longrun

3 null

2 sprint

实际上我想要“跑步者”旁边的o / p ..有人请帮帮我

2 个答案:

答案 0 :(得分:0)

你可以尝试一下吗?

select v, substr(regexp_substr(v1, ':[^:]*:runner'), 1, -7) from TABLE_NAME;

(警告:我担心我还没有掌握Oracle数据库,所以我自己没有对此进行过测试)

答案 1 :(得分:0)

这是我的解决方案,

WITH x AS (SELECT '4 hurdle:longrun:runner:rod:robi' AS v_string FROM dual UNION ALL
                  SELECT '4 hurdle:longrun:runner:rod:robi' AS v_string FROM dual UNION ALL
                  SELECT '3 longrun:jog:rod:robi' AS v_string FROM dual UNION ALL
                  SELECT '2 longrun: sprint :runner:rod:robi' AS v_string FROM dual )

select 
substr(v_string,1,2)||
substr(v_string,
       instr(v_string,':',1)+1,
       instr(
             substr(v_string,
                    instr(v_string,':',1)+3 ),
             'runner')) from x;

OR

WITH x AS (SELECT '4 hurdle:longrun:runner:rod:robi' AS v_string FROM dual UNION ALL
                  SELECT '4 hurdle:longrun:runner:rod:robi' AS v_string FROM dual UNION ALL
                  SELECT '3 longrun:jog:rod:robi' AS v_string FROM dual UNION ALL
                  SELECT '2 longrun: sprint :runner:rod:robi' AS v_string FROM dual )

SELECT replace(substr(v_string,1,instr(v_string, ':runner')-1),
               substr(
                      substr(v_string,1,instr(v_string, ':runner')-1),  
                      3,
                      instr(
                            substr(v_string,1,instr(v_string, ':runner')-1),
                            ':',
                             1)-2)
                      )from x;