根據需求,寫了一段方法。
用于識别以下的情況:
判斷 字元串A 在用逗号分隔的字元串B中是否存在
如:
v_str_a = aa ;
v_str_b= aa,bb,dd,cc ;
如上,就傳回Y,否則傳回N。
添加了一些校驗。
以後可以根據需求,按照指定的分隔符,提取字元串。
畢竟Oracle的字元串解析比較麻煩,能封裝就封裝。
create or replace function func_str_inArray(p_target varchar2,
p_str_array varchar2)
return varchar2 is
v_flag varchar2(1);
v_comma_loc int;
v_cut_string varchar2(300);
v_rest_string varchar2(2000);
begin
------------------------
--p_target 不能包含","!!!注意!!
--info:這個函數用于識别目标字元串,是否在一串用“,”分開的字元串内
------------------------
v_flag := 'N';
v_comma_loc := instr(p_str_array, ',');
--如果是對比字元串是空,則傳回false
if nvl(p_str_array, '') = '' then
return 'N';
end if;
--如果沒有逗号,直接比較
if length(p_str_array) > 0 and v_comma_loc = 0 then
if p_target = p_str_array then
return 'Y';
else
return 'N';
end if;
end if;
v_rest_string := p_str_array;
while v_comma_loc > 0 loop
v_cut_string := substr(v_rest_string, 0, v_comma_loc - 1);
v_rest_string := substr(v_rest_string,
v_comma_loc + 1,
length(v_rest_string) - 1);
if p_target = v_cut_string then
v_flag := 'Y';
end if;
v_comma_loc := instr(v_rest_string, ',');
if v_comma_loc = 0 and length(v_rest_string) > 0 then
if p_target = v_rest_string then
v_flag := 'Y';
end if;
end if;
end loop;
return v_flag;
end;