Get value between 2nd and 3rd comma

前端 未结 2 1320
执笔经年
执笔经年 2020-12-12 06:39

I am trying to extract the state from an address where everything is in one column, heres an example:

2901 MAIN ST,CORNING,NY,14830

I have

2条回答
  •  暖寄归人
    2020-12-12 07:06

    Regular expressions are a great way to do this sort of thing. SUBSTR and INSTR can also be used, however, by taking advantage of the 4th parameter of INSTR, nth_appearance:

    select INSTR(mystring,',',1,1) AS first_comma
          ,INSTR(mystring,',',1,2) AS second_comma
          ,SUBSTR(mystring
                 ,INSTR(mystring,',',1,1) + 1
                 ,INSTR(mystring,',',1,2)
                  - INSTR(mystring,',',1,1)
                  - 1)
           AS middle_bit
    FROM
    (select 'hello,world,NY,11725-1234' as mystring from dual);
    
    FIRST_COMMA  SECOND_COMMA  MIDDLE_BIT
    ===========  ============  ==========
              6            12  world
    

提交回复
热议问题