Remove repeated string functions in Oracle stored procedures

Source: Internet
Author: User

The following functions are compiled inWrite OracleDatabaseStorageI believe that the functions written in the process will be very helpful to you. What is important is that the functions are quite versatile. Therefore, I would like to share them with you for your reference, I hope this will help you.

This function is mainly used to remove repeated strings in a given string. You need to specify the separator of the string in use. Example:

Str: = MyReplace ('13, 14, 13, 444 ',',');

Output:

444

Create or replace function MyReplace (oldStr varchar2, sign varchar2) return varchar2 is

Str varchar2 (1000 );

CurrentIndex number;

StartIndex number;

EndIndex number;

Type str_type is table of varchar2 (30)

Index by binary_integer;

Arr str_type;

Result varchar2 (1000 );

Begin

If oldStr is null then

Return ('');

End if;

Str: = oldStr;

CurrentIndex: = 0;

StartIndex: = 0;

Loop

CurrentIndex: = currentIndex + 1;

EndIndex: = instr (str, sign, 1, currentIndex );

If (endIndex <= 0) then

Exit;

End if;

Arr (currentIndex): = trim (substr (str, startIndex + 1, endIndex-startIndex-1 ));

StartIndex: = endIndex;

End loop;

Take the last string:

Arr (currentIndex): = substr (str, startIndex + 1, length (str ));

Remove the repeated strings:

For I in 1 .. currentIndex-1 loop

For j in I + 1 .. currentIndex loop

If arr (I) = arr (j) then

Arr (j): = '';

End if;

End loop;

End loop;

Str: = '';

For I in 1 .. currentIndex loop

If arr (I) is not null then

Str: = str | sign | arr (I );

Empty array:

Arr (I): = '';

End if;

End loop;

Remove the preceding identifier:

Result: = substr (str, 2, length (str ));

Return (Result );

End MyReplace;

Here, the explanation of removing the repeated string functions in the Oracle stored procedure is complete. I hope you can learn from the above.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.