instr()函数的格式
格式一:instr( string1, string2 ) // instr(源字符串, 目标字符串)
解析:检索string2第一次出现在string1中的位置。
例子:
1.默认第一次出现“l”的位置
select instr('helloworld','l') from dual;
--返回结果:3
2.即“ow”同时出现,第一个字母“o”出现的位置
select instr('helloworld','ow') from dual;
--返回结果:5
格式二:
instr( string1, string2,[开始脚标 ],[ 第几次出现] ] )
解析:string2 的值要在string1中查找,是从"开始脚标"给出的数值(即:位置)开始在string1检索,检索"第几次出现"的string2的位置。
例子:
1.在"helloworld"的第4(l)号位置开始,查找第二次出现的“l”的位置
select instr('helloworld','l',4,2) from dual;
–返回结果:9
2.在"helloworld"的倒数第1(d)号位置开始,往回查找第一次出现的“l”的位置
select instr('helloworld','l',-1,1) from dual;
–返回结果:9