Home Database Mysql Tutorial A relatively complex stored procedure for splitting strings multiple times

A relatively complex stored procedure for splitting strings multiple times

Feb 16, 2017 pm 01:19 PM

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;
Copy after login



##begin

  var_tmp := var_str;
  var_instr_first :=0;
  var_instr_second :=0;
  var_result :='';
  var_length:=0;
Copy after login


## /* 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||'  ');
Copy after login


/* 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;);
Copy after login


/* 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;);
Copy after login



/* 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;);
Copy after login



/* 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;);
Copy after login



/* 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;
Copy after login




-- 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);
Copy after login


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;
Copy after login

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


Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Steps and precautions for implementing batch updates using Oracle stored procedures Steps and precautions for implementing batch updates using Oracle stored procedures Mar 08, 2024 pm 04:12 PM

Title: Steps and Precautions for Implementing Batch Updates by Oracle Stored Procedures In Oracle database, stored procedures are a set of SQL statements designed to improve database performance, reuse code, and enhance security. Stored procedures can be used to update data in batches. This article will introduce how to use Oracle stored procedures to implement batch updates and provide specific code examples. Step 1: Create a stored procedure First, we need to create a stored procedure to implement batch update operations. The following is how to create a stored procedure

How to determine whether a Golang string ends with a specified character How to determine whether a Golang string ends with a specified character Mar 12, 2024 pm 04:48 PM

Title: How to determine whether a string ends with a specific character in Golang. In the Go language, sometimes we need to determine whether a string ends with a specific character. This is very common when processing strings. This article will introduce how to use the Go language to implement this function, and provide code examples for your reference. First, let's take a look at how to determine whether a string ends with a specified character in Golang. The characters in a string in Golang can be obtained through indexing, and the length of the string can be

Detailed explanation of the method of converting int type to string in PHP Detailed explanation of the method of converting int type to string in PHP Mar 26, 2024 am 11:45 AM

Detailed explanation of the method of converting int type to string in PHP In PHP development, we often encounter the need to convert int type to string type. This conversion can be achieved in a variety of ways. This article will introduce several common methods in detail, with specific code examples to help readers better understand. 1. Use PHP’s built-in function strval(). PHP provides a built-in function strval() that can convert variables of different types into string types. When we need to convert int type to string type,

How to repeat a string in python_python repeating string tutorial How to repeat a string in python_python repeating string tutorial Apr 02, 2024 pm 03:58 PM

1. First open pycharm and enter the pycharm homepage. 2. Then create a new python script, right-click - click new - click pythonfile. 3. Enter a string, code: s="-". 4. Then you need to repeat the symbols in the string 20 times, code: s1=s*20. 5. Enter the print output code, code: print(s1). 6. Finally run the script and you will see our return value at the bottom: - repeated 20 times.

How to check if a string starts with a specific character in Golang? How to check if a string starts with a specific character in Golang? Mar 12, 2024 pm 09:42 PM

How to check if a string starts with a specific character in Golang? When programming in Golang, you often encounter situations where you need to check whether a string begins with a specific character. To meet this requirement, we can use the functions provided by the strings package in Golang to achieve this. Next, we will introduce in detail how to use Golang to check whether a string starts with a specific character, with specific code examples. In Golang, we can use HasPrefix from the strings package

How to intercept a string in Go language How to intercept a string in Go language Mar 13, 2024 am 08:33 AM

Go language is a powerful and flexible programming language that provides rich string processing functions, including string interception. In the Go language, we can use slices to intercept strings. Next, we will introduce in detail how to intercept strings in Go language, with specific code examples. 1. Use slicing to intercept a string. In the Go language, you can use slicing expressions to intercept a part of a string. The syntax of slice expression is as follows: slice:=str[start:end]where, s

How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP How to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP Mar 04, 2024 am 09:36 AM

Methods to solve Chinese garbled characters when converting hexadecimal strings in PHP. In PHP programming, sometimes we encounter situations where we need to convert strings represented by hexadecimal into normal Chinese characters. However, in the process of this conversion, sometimes you will encounter the problem of Chinese garbled characters. This article will provide you with a method to solve the problem of Chinese garbled characters when converting hexadecimal to string in PHP, and give specific code examples. Use the hex2bin() function for hexadecimal conversion. PHP’s built-in hex2bin() function can convert 1

Oracle stored procedure: Implementation method to determine whether a table exists Oracle stored procedure: Implementation method to determine whether a table exists Mar 08, 2024 pm 09:18 PM

Stored procedures in Oracle database are a specific type of stored procedures used to execute a series of SQL statements and data operations in the database. In actual database development work, sometimes we need to determine whether a certain table exists in the database, so that we can do some judgment and logical processing in the storage process. Below we will introduce how to implement the method of determining whether a table exists in Oracle database, and provide specific code examples. First, we can use the system table user_tables or all_t

See all articles