例如存在两个字符串 str1=001;002;004;006 str2 =005 str3= 004 str4=001,007
函数名: f_findchar(stra varchar2(100),strb varchar(100))
函数说明:字符串str1 的子字符串 中是否包含str2 中的子字符串
,如果包含返回1,不包含返回0
select f_findchar(str1,str2 ) from dual; 返回 0
select f_findchar(str1,str3) from dual; 返回 1
select f_findchar(str1,str4 ) from dual; 返回 1
select f_findchar(str4,str2 ) from dual; 返回 0这个函数如何写?谢谢指教;
函数名: f_findchar(stra varchar2(100),strb varchar(100))
函数说明:字符串str1 的子字符串 中是否包含str2 中的子字符串
,如果包含返回1,不包含返回0
select f_findchar(str1,str2 ) from dual; 返回 0
select f_findchar(str1,str3) from dual; 返回 1
select f_findchar(str1,str4 ) from dual; 返回 1
select f_findchar(str4,str2 ) from dual; 返回 0这个函数如何写?谢谢指教;
SQL> with t as(
2 select '001;002;004;006' str1,'005' str2,'004' str3,'001,007' str4 from dual)
3 select case when instr(str1,str2)>0 then 1 else 0 end c1,
4 case when instr(str1,str3)>0 then 1 else 0 end c2,
5 case when instr(str1,str4)>0 then 1 else 0 end c3,
6 case when instr(str4,str2)>0 then 1 else 0 end c4
7 from t
8 /
C1 C2 C3 C4
---------- ---------- ---------- ----------
0 1 0 0
return integer as
strtmp varchar2(4000);
num integer;
begin
num := length(str2) - length(replace(str2, ';')) + 1;
if num is null then
return 0;
end if;
for x in 1 .. num loop
strtmp := substr(str2,
instr(';' || str2, ';', 1, x),
instr(str2 || ';', ';', 1, x) -
instr(';' || str2, ';', 1, x));
if instr(';' || str1 || ';', ';' || strtmp || ';') > 0 then
return 1;
end if;
end loop;
return 0;
end;字符串里面的分隔符最好统一一下。上面的例子用";"作分隔符
楼主确定你的字符串中是以分号隔开的吗?
create or replace function mw_app.compare_two_str(p_str in varchar2,
p_substr in varchar2)
return integer is
v_result integer;
v_index integer;
v_num integer := 0;
v_tempstr varchar2(500) := p_substr;
v_replace varchar2(100);
type stringnest is table of varchar2(100);
v_string stringnest;
/*前面的字符串中如果包含后边的字符串数组中的任意一个,返回1,否则返回0*/
begin
v_string := stringnest();
/* if (instr(p_str, p_substr, 1, 1) > 0) then
return 1;
end if;*/
loop
v_index := instr(v_tempstr, ';', 1, 1);
v_num := v_num + 1;
if (v_index > 0) then
v_string.extend;
v_string(v_num) := substr(v_tempstr, 1, v_index - 1);
v_replace := substr(v_tempstr, 1, v_index);
v_tempstr := replace(v_tempstr, v_replace, '');
else
v_string.extend;
v_string(v_num) := v_tempstr;
end if;
exit when(v_index = 0);
end loop;
v_index := v_string.first;
loop
exit when v_index is null;
if (instr(p_str, v_string(v_index), 1, 1) > 0) then
return 1;
end if;
dbms_output.put_line(v_string(v_index));
v_index := v_string.next(v_index);
end loop;
return 0;
end;