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!