INSTR方法的格式为
INSTR(src, subStr,startIndex, count)
src: 源字符串
subStr : 要查找的子串
startIndex : 从第几个字符开始。负数表示从右往左查找。
count: 要找到第几个匹配的序号
返回值: 子串在字符串中的位置,第1个为1;不存在为0. (特别注意:如果src为空字符串,返回值为null)。
用法举例:
最简单的一种,查找l字符,首个l位于第3个位置。
SQL> select instr('hello,java world', 'l') from dual;
INSTR('HELLO,JAVAWORLD','L')
----------------------------
3
查找l字符,从第4个位置开始。
SQL> select instr('hello,java world', 'l', 4) from dual;
INSTR('HELLO,JAVAWORLD','L',4)
------------------------------
4
查找l字符,从第1个位置开始的第3个
SQL> select instr('hello,java world', 'l', 1, 3) from dual;
INSTR('HELLO,JAVAWORLD','L',1,
------------------------------
15
查找l字符,从右边第1个位置开始,从右往左查找第3个(也即是从左到右的第1个)
SQL> select instr('hello,java world', 'l', -1, 3) from dual;
INSTR('HELLO,JAVAWORLD','L',-1
------------------------------
3
找不到返回0
SQL> select instr('hello,java world', 'MM') from dual;
INSTR('HELLO,JAVAWORLD','MM')
-----------------------------
0
源字符为空字符串''的情况
-- Created on 2010-12-22 by CHEN
declare
-- Local variables here
i varchar2(2);
begin
-- Test statements here
i := instr('',',');
if i is null then
dbms_output.put_line(' i is empty');
end if;
end;
结果输出:
i is empty