Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Splitting test string via underscore

user16854Nov 30 2010 — edited Nov 30 2010
I need to split some text strings into columns, using the underscore as a delimiter.

I've got so far, via:
GET sampledata
WITH sampledata AS
     (SELECT 'TEST_PPL_AM_POR' test_key
        FROM DUAL
      UNION ALL
      SELECT 'TEST_CTY_GL_POR'
        FROM DUAL
      UNION ALL
      SELECT 'TEST_CF1_AP_POR'
        FROM DUAL)
SELECT test_key
     , LENGTH(test_key) v1
     , SUBSTR(test_key,0,INSTR(test_key, '_')-1) v2
     , SUBSTR(test_key, INSTR(test_key, '_') + 1, INSTR(test_key, '_') - 2) v3
  FROM sampledata;

TEST_KEY                V1 V2              V3             
--------------- ---------- --------------- ---------------
TEST_PPL_AM_POR         15 TEST            PPL            
TEST_CTY_GL_POR         15 TEST            CTY            
TEST_CF1_AP_POR         15 TEST            CF1         
I can get the first 2 parts split into columns, but then get rapidly confused with the nested INSTRs.

I wondered if there is a simpler route / a better way of doing this?

Any advice much appreciated,

Thanks!
This post has been answered by Frank Kulash on Nov 30 2010
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 28 2010
Added on Nov 30 2010
5 comments
5,410 views