Home  >  Article  >  Database  >  A relatively complex stored procedure for splitting strings multiple times

A relatively complex stored procedure for splitting strings multiple times

黄舟
黄舟Original
2017-02-16 13:19:571347browse

The special split function I wrote is as follows:
##create or replace function FN_SPLIT_STR_2(var_str in varchar2)return varchar2
/*
There is a table t1 with a field called c3, which stores the location information of all stores.
Now we need a stored procedure to reduce the coordinate values ​​of the c3 field of all records by 3 times and write the content of the c field


For example, 220.25 257,220.25 269.75,229.25 269.75,229.25 257
Each comma separates the coordinate points, and each coordinate point uses a space to distinguish the x coordinate and y coordinate


Storage function name: FN_SPLIT_STR_2
Purpose: Reduce bis_store coordinates v_coords3 three times and update coords, such as
author:huangshan
*/

as
  var_tmp     varchar2(4000);
  var_element varchar2(4000);
  var_result varchar2(4000);
  var_instr_first number;
  var_instr_second number;
  var_length number;



##begin

  var_tmp := var_str;
  var_instr_first :=0;
  var_instr_second :=0;
  var_result :='';
  var_length:=0;


## /* Replace the passed Special characters
chr (9) Tab character
chr (10) Carriage return
chr (13) Line feed
*/
##

  var_tmp:= replace(var_tmp,chr(10),'');
  var_tmp:= replace(var_tmp,chr(13),'');
  var_tmp:= replace(var_tmp,chr(9),'');
  
  while instr(var_tmp, ' ') > 0 
    or instr(var_tmp, ',')>0 
    or(var_length>0) loop
    var_instr_first :=instr(var_tmp, ' ');
    var_instr_second :=instr(var_tmp, ',');
   -- dbms_output.put_line('var_instr_kg:'||var_instr_first||'  ');
   -- dbms_output.put_line('var_instr_dh:'||var_instr_second||'  ');
         var_length:=length(var_tmp);
   --  dbms_output.put_line('var_length  :'||var_length||'  ');


/* 1 If there is a space first, such as 12 32, 12 32, etc. **/

  if var_instr_first<var_instr_second  then
         var_element := round(to_number(substr(var_tmp, 1, var_instr_first-1))/3,2);
         var_result := var_result|| var_element|| &#39; &#39;;
         var_tmp := substr(var_tmp,var_instr_first+1, length(var_tmp));
      --   dbms_output.put_line(&#39;var_result kg:&#39;||var_result);
      --   dbms_output.put_line(&#39;var_tmp kg:&#39;||var_tmp||&#39;  &#39;);
      --   dbms_output.put_line(&#39;var_element kg:&#39;||var_element||&#39;  &#39;);


/* 2 If the spaces have been intercepted, the comma is in front, such as 32,12 32**/

  elsif var_instr_first>var_instr_second and  var_instr_second>0 then
           var_element := round(to_number(substr(var_tmp, 1, var_instr_second-1))/3,2);
           var_result := var_result || var_element || &#39;,&#39; ;
           var_tmp := substr(var_tmp,var_instr_second+1, length(var_tmp));
         --  dbms_output.put_line(&#39;var_result dh:&#39;||var_result);
        --   dbms_output.put_line(&#39;var_tmp dh:&#39;||var_tmp||&#39;  &#39;);
        --   dbms_output.put_line(&#39;var_element dh:&#39;||var_element||&#39;  &#39;);



/* 3 如果是已经截取完逗号,已经只剩下最后一个坐标x y,比如12 32这类 **/  
   

 elsif var_instr_first>var_instr_second and  var_instr_second=0 then
         var_element := round(to_number(substr(var_tmp, 1, var_instr_first-1))/3,2);
         var_result := var_result|| var_element|| &#39; &#39;;
         var_tmp := substr(var_tmp,+1, length(var_tmp));
       --  dbms_output.put_line(&#39;var_result kg:&#39;||var_result);
       --  dbms_output.put_line(&#39;var_tmpvar_instr_first kg:&#39;||var_tmp||&#39;  &#39;);
        -- dbms_output.put_line(&#39;var_element kg:&#39;||var_element||&#39;  &#39;);


        
/* 4 如果是已经截取到最后一个坐标,比如32这类 **/ 
 

    elsif var_instr_first=0 and var_instr_second=0 and var_length>0 then
      --  dbms_output.put_line(&#39;var_tmp the last one:&#39;||var_tmp||&#39;  &#39;);
       var_element := round(to_number(var_tmp)/3,2);
       var_result := var_result  || var_element;
       var_tmp:=&#39;&#39;;
       --  dbms_output.put_line(&#39;var_result 0:&#39;||var_result);
       --  dbms_output.put_line(&#39;var_tmp 0:&#39;||var_tmp||&#39;  &#39;);
       --  dbms_output.put_line(&#39;var_element 0:&#39;||var_element||&#39;  &#39;);


 
/* 5 如果其他的东西,设置成''退出while循环为止 **/ 

     else 
       var_tmp:=&#39;&#39;;
     end if;
    -- dbms_output.put_line(&#39;     &#39;);
     
  end loop;
  return var_result;
end FN_SPLIT_STR_2;




-- google其他人的拆分function如下:
-- 拆分函数

create or replace function split_str(var_str   in varchar2, 
                                       var_split in varchar2) 
/**************************************************** 
  注意 先执行下面语句 创建类型 
  create or replace type t_ret_table is table of varchar2(100) 
  ** 函数名称:split_str 
  ** 参    数:【名称】         【类型 】      【说明】 
  **           var_str          varchar2       要拆分的字符串 
  **           var_split        varchar2       字符串分隔符 
  ** 返 回 值:Result           t_ret_table    拆分后数组集合 
  ** 摘    要:拆分字符串 
  调用 举例: 
  select * from table(split_str(&#39;2008-10-21&#39;,&#39;-&#39;)) 
  ****************************************************/ 
  return t_ret_table is 
  var_out     t_ret_table; 
  var_tmp     varchar2(4000); 
  var_element varchar2(4000);


begin 
 

 var_tmp := var_str; 
  var_out := t_ret_table(); 
  --如果存在匹配的分割符 
  while instr(var_tmp, var_split) > 0 loop 
    var_element := substr(var_tmp, 1, instr(var_tmp, var_split) - 1); 
    var_tmp     := substr(var_tmp, 
                          instr(var_tmp, var_split) + length(var_split), 
                          length(var_tmp)); 
    --var_out.extend(1); 
    var_out.extend; 
    var_out(var_out.count) := var_element; 
  end loop; 
  --var_out.extend(1); 
  var_out.extend; 
  var_out(var_out.count) := var_tmp; 
  return var_out; 
end split_str;

 以上就是一个比较复杂的多次拆分字符串的存储过程的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn