以下函數是本人在編寫Oracle數據庫存儲過程時寫的函數,覺得該函數通用性較強,因此發表出來供需要的人參考.
這個函數的功能主要是用于去除給定字符串中重復的字符串.在使用中需要指定字符串的分隔符.示例:
str := MyReplace('13,14,13,444', ',');
輸出:
13,14,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;
?
? --取最后一個字符串
? arr(currentIndex) := substr(str, startIndex + 1, length(str));
?
? --去掉重復出現的字符串
? 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);
???
??? --數組置空
??? arr(i) := '';
? end if;
? end loop;
?
? --去掉前面的標識符
? Result := substr(str, 2, length(str));
? return(Result);
end MyReplace;?
?
這個函數的功能主要是用于去除給定字符串中重復的字符串.在使用中需要指定字符串的分隔符.示例:
str := MyReplace('13,14,13,444', ',');
輸出:
13,14,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;
?
? --取最后一個字符串
? arr(currentIndex) := substr(str, startIndex + 1, length(str));
?
? --去掉重復出現的字符串
? 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);
???
??? --數組置空
??? arr(i) := '';
? end if;
? end loop;
?
? --去掉前面的標識符
? Result := substr(str, 2, length(str));
? return(Result);
end MyReplace;?
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
