基于oracle中的分隔符的字符串拆分

时间:2013-03-13 16:26:08

标签: oracle plsql oracle10g

我在oracle表中有这样的字符串

VM4 SPLA - WI NS V R STD #P59- 9 9 99 QTY 2 : S P LA - W IN SV RENT #P39-9999 QTY 3 : SPL A - WIN S VR SMB # P 3 9- 999 99 QTY 5    1

我需要拆分此字符串并使用循环

插入表中
name description           partnumber   Qty  objectstate
-------------------------------------------------------
VM4  SPLA - WI NS V R STD  P59- 9 9 99   2    1
VM4  S P LA - W IN SV RENT P39-9999      3    1
VM4  SPL A - WIN S VR SMB  P 3 9- 999 99 5    1

使用循环帮助我如何做到这一点。

1 个答案:

答案 0 :(得分:0)

declare
  string varchar2(4000);
  v_objstate number;
  v_name varchar2(100);
  v_description varchar2(100);
  v_partnumber varchar2(100);
  v_qty number;
  v_all_rows varchar2(4000);
  v_row varchar2(4000);
begin
  string := 'VM4 SPLA - WI NS V R STD #P59- 9 9 99 QTY 2 : S P LA - W IN SV RENT #P39-9999 QTY 3 : SPL A - WIN S VR SMB # P 3 9- 999 99 QTY 5    1';
  v_objstate := to_number(regexp_substr(string, '\d+$'));
  v_all_rows := regexp_replace(string, '\d+$');
  v_name := regexp_substr(v_all_rows, '\S+');
  v_all_rows := regexp_replace(v_all_rows, '^\s*\S+');
  for rec in (
    select regexp_substr(v_all_rows, '[^:]+', 1, level) as next_row 
    from dual connect by null is null
  ) loop
    v_row := trim(rec.next_row);
    exit when v_row is null;
    v_qty := to_number(trim(regexp_substr(v_row, '\d+\s*$')));
    v_row := regexp_replace(v_row, 'QTY\s*\d+\s*$');
    v_partnumber := trim(regexp_replace(v_row, '^.*#'));
    v_description := trim(regexp_replace(v_row, '#.*?$'));
    insert into your_table (name, description, partnumber, Qty, objectstate) 
      values (v_name, v_description, v_partnumber, v_qty, v_objstate);
  end loop;
end;