获取介于8到数值之间的字符:Oracle

时间:2018-12-20 06:56:52

标签: sql oracle

我有以下字符串

"923449499950,49,3513,NewBasic5,Talkshawk,IDKR17,North-2,KARAK,"Village Warana Mir Hassan Khel ,Dist Karak Tehsil,459,N2-NKRK1201,.0000,.0000,.00,.00,.00,.00,0,.0000,.0000,.0000,.00,.00,.0000,.00,.00,Nokia,108,Basic-Voice Only,Proprietary OS,Nokia 108"

我想在KARAK和459之间提取字符串。

我无法破解,到目前为止,我使用“,”作为分隔符完成了以下操作

SELECT nth_string
FROM
  (SELECT TRIM (REGEXP_SUBSTR (long_string, '[^,]+', 1, ROWNUM) ) nth_string ,
    level AS lvl
  FROM
    (SELECT REPLACE('923449499950,49,3513,NewBasic5,Talkshawk,IDKR17,North-2,KARAK,"Village Warana Mir Hassan Khel ,Dist Karak Tehsil,459,N2-NKRK1201,.0000,.0000,.00,.00,.00,.00,0,.0000,.0000,.0000,.00,.00,.0000,.00,.00,Nokia,108,Basic-Voice Only,Proprietary OS,Nokia 108',
    ',,',', ,') long_string
    FROM DUAL
    )
    CONNECT BY LEVEL <= REGEXP_COUNT ( long_string, '[^,]+')
  )WHERE lvl = 9;

因此,结果字符串将为“ Varage Warana Mir Hassan Khel,Dist Karak Tehsil”。 我使用的代码是“乡村Warana Mir Hassan Khel”。

1 个答案:

答案 0 :(得分:1)

这里是一个选择:

SQL> with test (col) as
  2    (select '"923449499950,49,3513,NewBasic5,Talkshawk,IDKR17,North-2,KARAK,"Village Warana Mir Hassan Khel ,Dist Kar
ak Tehsil,459,N2-NKRK1201,.0000,.0000,.00,.00,.00,.00,0,.0000,.0000,.0000,.00,.00,.0000,.00,.00,Nokia,108,Basic-Voice On
ly,Proprietary OS,Nokia 108"'
  3    from dual)
  4  select instr(col, ',', 1, 8) + 1 eig,            --> 8th comma position
  5         regexp_instr(col, '\d+', 1, 7) dig,       --> digit (459 in your example)
  6  --
  7  rtrim(ltrim(substr(col,
  8                     instr(col, ',', 1, 8) + 1 ,        --> starting position
  9                     regexp_instr(col, '\d+', 1, 7) - 1 - instr(col, ',', 1, 8) - 1 ),          --> length
 10        '"'), '"') result          --> remove double quotes (if any)
 11  from test;

       EIG        DIG RESULT
---------- ---------- -------------------------------------------------
        64        115 Village Warana Mir Hassan Khel ,Dist Karak Tehsil

您需要的是第7-10行。