========================================================== ======================== Author: hanjs (http://hanjs.itpub.net)
Posted:
Category: oracle
Source: http://hanjs.itpub.net/post/28217/260065
---------------------------------------------------------------
The following is an article about string processing in zhouwf0726 on itpub, but I personally think it is more common to write a word-defined function.
/*
How to remove repeated characters in a string
For example, 'How do I remove the repeated characters in the string with commas and sort the characters in ascending order?
', 1, 2, 3, 5, 9 ,'
I think it's hard to explain. It's a good expert to give it a try.
Answer:
Select col from (
Select sys_connect_by_path (col, ',') | ', 'col, level from (
Select col, row_number () over (order by rownum) rn from (
Select distinct substr (col, instr (col, ',', 1, rownum) + 1, instr (col, ',', 1, rownum + 1)-instr (col, ',', 1, rownum)-1) col from (
Select ', 'col from dual
) Connect by rownum <length (translate (col, ',' | col ,','))
)
)
Connect by prior rn = rn-1 order by level desc
) Where rownum = 1
*/
Use a custom function to implement 1. Use the function to return an array.
Create or replace function f_split_string (var_str in string, var_split In String) 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 ();
-- If a matched delimiter exists
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 (var_out.count): = var_element;
End loop;
Var_out.extend (1 );
Var_out (var_out.count): = var_tmp;
Return var_out;
End f_split_string;
/*
Create or replace type t_ret_table is table of varchar2 (20 );
Returns an array, but can also be queried using a statement.
Select * from table (f_split_string ('hanjs-zhaos ','-'));
*/
Use the pipelined Function.
Create or replace function f_split (var_str in string, var_split In String) return t_ret_table PIPELINED
As
Var_tmp varchar2 (4000 );
Var_element varchar2 (4000 );
N_length Number: = length (var_split );
Begin
Var_tmp: = var_str;
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) + n_length, length (var_tmp ));
Pipe row (var_element );
End loop;
Pipe row (var_tmp );
Return;
End f_split;
/*
Create or replace type t_ret_table is table of varchar2 (20 );
String-separated functions, which are queried using statements
Select * from table (f_split ('hanjs-Zhaos ','-'));
*/
The sorting can be fully reflected in the query.
Note: The queried column name is column_value.