Get value between 2nd and 3rd comma

前端 未结 2 1318
执笔经年
执笔经年 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条回答
  •  -上瘾入骨i
    2020-12-12 06:51

    select 
      regexp_substr('2901 MAIN ST,CORNING,NY,14830', '(.*?,){2}(.*?),', 1, 1, '', 2) 
    from dual
    

    In general,

    n_th_component := 
      regexp_substr(string, '(.*?,){'||(n-1)||'}([^,]*)', 1, 1, '', 2);
    

    Example:

    select 
      n,  
      regexp_substr('2901 MAIN ST,CORNING,NY,14830', 
                    '(.*?,){'||(n-1)||'}([^,]*)', 1, 1, '', 2)
    from (select level n from dual connect by level <= 4)
    

提交回复
热议问题