以固定分隔符拆分字符串

  在做项目时,经常要读取txt记录文件数据,并要求按照固定格式进行拆分,所以编写了如下的存储过程:

点击(此处)折叠或打开

  1. PROCEDURE Str_To_Arr(p_Source_Str IN VARCHAR2,
  2.                        p_Split_Str IN VARCHAR2,
  3.                        Errbuf OUT VARCHAR2,
  4.                        Retcode OUT VARCHAR2,
  5.                        p_Dens_Array OUT Type_Tab_Arr) IS

  6.     v_Source_Str VARCHAR2(1000);
  7.     v_Split_Str VARCHAR2(8);
  8.     v_Count NUMBER; ----分割的个数
  9.     v_Split_Local NUMBER; ----分割符出现的位置
  10.     v_Pos_Local NUMBER; ----分割起始位置
  11.     v_Tab_Arr Xxt_Gl_Load_Vat_Pkg.Type_Tab_Arr;

  12.   BEGIN
  13.     Retcode := 0;
  14.     v_Pos_Local := 1;
  15.     v_Count := 1;
  16.     v_Source_Str := p_Source_Str;
  17.     v_Split_Str := p_Split_Str;
  18.     IF Length(v_Source_Str) = 0 THEN
  19.       Log('判断1');
  20.       Errbuf := '要分割的字符串为空,请输入要分割的字符串!';
  21.       Retcode := '2';
  22.       RETURN;
  23.     END IF;
  24.     IF Length(v_Split_Str) = 0 THEN
  25.       Log('判断2');
  26.       Errbuf := '请输入要分割的界定符!';
  27.       Retcode := '2';
  28.       RETURN;
  29.     END IF;
  30.     ---取出字符串的个数
  31.     SELECT Length(v_Source_Str) -
  32.            Length(REPLACE(Translate(v_Source_Str, v_Split_Str, ' '),
  33.                           ' ',
  34.                           '')) / Length(v_Split_Str)
  35.       INTO v_Count
  36.       FROM Dual;
  37.     FOR i IN 1 .. v_Count LOOP

  38.       SELECT Instr(v_Source_Str, v_Split_Str, v_Pos_Local + 1, 1)
  39.         INTO v_Split_Local
  40.         FROM Dual;

  41.       IF v_Split_Local = 0 THEN

  42.         v_Tab_Arr(i) := v_Source_Str;

  43.         dbms_output.put_line(v_tab_arr(i));
  44.         EXIT;
  45.       ELSE
  46.         IF i = 1 THEN
  47.           v_Tab_Arr(i) := Substr(v_Source_Str,
  48.                                  v_Pos_Local,
  49.                                  v_Split_Local - v_Pos_Local);
  50.           v_Pos_Local := v_Split_Local;
  51.           Dbms_Output.Put_Line(v_Tab_Arr(i));

  52.         ELSE
  53.           v_Tab_Arr(i) := Substr(v_Source_Str,
  54.                                  v_Pos_Local + Length(v_Split_Str),
  55.                                  v_Split_Local -
  56.                                  (v_Pos_Local + Length(v_Split_Str)));
  57.           v_Pos_Local := v_Split_Local;

  58.           --Dbms_Output.Put_Line(v_Tab_Arr(i));
  59.         END IF;
  60.       END IF;
  61.     END LOOP;
  62.     p_Dens_Array := v_Tab_Arr;
  63.   END Str_To_Arr;


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29507357/viewspace-2124552/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29507357/viewspace-2124552/

你可能感兴趣的:(以固定分隔符拆分字符串)