LuckyLucks
asked on
instr twice to substring variable length substring in oracle
HI
I have a column that has a substring following 1st "_" and before 2nd "_" that I want parsed out.
1234567890123_ABCD_AN_XX_S RCV_A
1234567890123_ABCDE_AN_XX_ FARB_B
So the substring I want is ABCD and ABCDE (this can be any length)
The digits are always 13 at the start of the full string. And there are 5 "_" in the full string always.
I have a column that has a substring following 1st "_" and before 2nd "_" that I want parsed out.
1234567890123_ABCD_AN_XX_S
1234567890123_ABCDE_AN_XX_
So the substring I want is ABCD and ABCDE (this can be any length)
The digits are always 13 at the start of the full string. And there are 5 "_" in the full string always.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
more dynamic -
SELECT SUBSTR('1234567890123_ABCD _AN_XX_SRC V_A',INSTR ('12345678 90123_ABCD _AN_XX_SRC V_A', '_', 1, 1)+1,INSTR('1234567890123_ ABCD_AN_XX _SRCV_A', '_', 1, 2)
- INSTR('1234567890123_ABCD_ AN_XX_SRCV _A', '_', 1, 1)-1) FROM dual
SELECT SUBSTR('1234567890123_ABCD E_AN_XX_FA RB_B',INST R('1234567 890123_ABC DE_AN_XX_F ARB_B', '_', 1, 1)+1,INSTR('1234567890123_ ABCDE_AN_X X_FARB_B', '_', 1, 2)
- INSTR('1234567890123_ABCDE _AN_XX_FAR B_B', '_', 1, 1)-1) FROM dual
Ouptut
1 ABCD
2 ABCDE
SELECT SUBSTR('1234567890123_ABCD
- INSTR('1234567890123_ABCD_
SELECT SUBSTR('1234567890123_ABCD
- INSTR('1234567890123_ABCDE
Ouptut
1 ABCD
2 ABCDE
REGEXP_SUBSTR(yourstring, '[^_]+', 15)
example usage...
SELECT yourstring, REGEXP_SUBSTR(yourstring, '[^_]+', 15)
FROM (SELECT '1234567890123_ABCD_AN_XX_ SRCV_A' yourstring FROM DUAL
UNION ALL
SELECT '1234567890123_ABCDE_AN_XX _FARB_B' FROM DUAL)
example usage...
SELECT yourstring, REGEXP_SUBSTR(yourstring, '[^_]+', 15)
FROM (SELECT '1234567890123_ABCD_AN_XX_
UNION ALL
SELECT '1234567890123_ABCDE_AN_XX
1 ABCD
2 ABCDE