【问题标题】:Split column in OracleOracle 中的拆分列
【发布时间】:2017-06-20 14:29:24
【问题描述】:

我在 Oracle 数据库中有一个列,其中包含类似这样的数据

column1
/opt/log/data/abcd.efghi.jklmn.aaa.txt
/opt/log/data/abbbcd.efccghi.jkdsdflmn.abab.txt
/opt/log/data/nmvcnmcd.efjhjghi.jkvslmn.abcbc.txt
/opt/log/data/hjsdhj.hjfdhdf.hdfhjd.aghag.txt
/opt/log/data/dfhjfdhj.yureyer.qwtyq.hjahjh.txt

我想以这样的方式拆分数据

**firstdot    seconddot      thirdnfourthdot**
abcd        efghi          jklmn.aaa
abbbcd      efccghi        jkdsdflmn.abab
nmvcnmcd    efjhjghi       jkvslmn.abcbc
hjsdhj      hjfdhdf        hdfhjd.aghag
dfhjfdhj    yureyer        qwtyq.hjahjh

我可以通过

得到seconddot的值
select substr(column1,instr(column1,'.',1+1,instr(column1,'.',1,2)-instr(column1,'.',1,1)-1) as secondot

但我无法得到其余的。你们能帮忙吗?

非常感谢

【问题讨论】:

    标签: oracle split substr


    【解决方案1】:

    如果不使用正则表达式,您需要为所需的每个子字符串回复相同的逻辑,每个 timi 都会根据该子字符串的“终止符”的位置来选择初始位置和长度。

    /* input data */
        with yourTable(column1) as (
            select '/opt/log/data/abcd.efghi.jklmn.aaa.txt' from dual union all
            select '/opt/log/data/abbbcd.efccghi.jkdsdflmn.abab.txt' from dual union all
            select '/opt/log/data/nmvcnmcd.efjhjghi.jkvslmn.abcbc.txt' from dual union all
            select '/opt/log/data/hjsdhj.hjfdhdf.hdfhjd.aghag.txt' from dual union all
            select '/opt/log/data/dfhjfdhj.yureyer.qwtyq.hjahjh.txt' from dual
        )
    /* query */
        select substr(column1, instr(column1, '/', -1) +1, instr(column1, '.') - instr(column1, '/', -1)-1) firstDot,
               substr(column1, instr(column1, '.') +1, instr(column1, '.', 1, 2) - instr(column1, '.') -1) secondDot,
               substr(column1, instr(column1, '.', 1, 2) +1, instr(column1, '.', 1, 4) - instr(column1, '.', 1, 2) -1) thirdAndFourthDot
        from yourTable 
    

    给予:

    FIRSTDOT        SECONDDOT       THIRDANDFOURTHD
    --------------- --------------- ---------------
    abcd            efghi           jklmn.aaa
    abbbcd          efccghi         jkdsdflmn.abab
    nmvcnmcd        efjhjghi        jkvslmn.abcbc
    hjsdhj          hjfdhdf         hdfhjd.aghag
    dfhjfdhj        yureyer         qwtyq.hjahjh
    

    以更易读的方式:

    select substr(column1, lastSlashPos +1, firstDotPos - lastSlashPos -1) as firstDot,
           substr(column1, firstDotPos +1, secondDotPos - firstDotPos -1) as secondDot,
           substr(column1, secondDotPos +1, fourthDotPos - secondDotPos -1) as thirdAndFourthDot
    from (
            select instr(column1, '/', -1) as lastSlashPos,
                   instr(column1, '.') as firstDotPos,
                   instr(column1, '.', 1, 2) as secondDotPos,
                   instr(column1, '.', 1, 3) as thirdDotPos,
                   instr(column1, '.', 1, 4) as fourthDotPos,
                   column1
            from yourTable
         ) 
    

    【讨论】:

      【解决方案2】:
      select substr('/opt/log/data/abcd.efghi.jklmn.aaa.txt',instr('/opt/log/data/abcd.efghi.jklmn.aaa.txt','/',-1) + 1) from dual;
      

      这将在最后一个/之后给你文本

      那你需要为.申请instr

      select 
        substr(text, 1,  instr(text,'.', 1) - 1), 
        substr(text, instr(text,'.', 1) + 1, instr(text,'.', 2) - 1), 
        substr(text, instr(text,'.', 2) + 1) 
      from (
          select substr('/opt/log/data/abcd.efghi.jklmn.aaa.txt',instr('/opt/log/data/abcd.efghi.jklmn.aaa.txt','/',-1) + 1) text from dual
      );
      

      【讨论】:

      • 在 from 语句中不使用 substr 函数是否可以做到这一点?
      猜你喜欢
      • 2017-01-12
      • 2021-10-20
      • 1970-01-01
      • 1970-01-01
      • 2015-10-06
      • 1970-01-01
      • 1970-01-01
      • 2015-08-22
      • 2015-07-23
      相关资源
      最近更新 更多