日期:2014-05-16  浏览次数:20515 次

instr函数的使用
对于instr函数,我们经常这样使用:从一个字符串中查找指定子串的位置。例如:
SQL> select instr('yuechaotianyuechao','ao') position from dual;

  POSITION
----------
         6

从字符串'yuechaotianyuechao'的第一个位置开始,向后查找第一个出现子串'ao'出现的位置。

其实instr共有4个参数,格式为“instr(string, substring, position, occurrence)”。可实现子串的如下搜索:
1.从指定位置开始搜索子串
2.指定搜索第几次出现的子串的位置
3.从后向前搜索

--1.从第7个字符开始搜索
SQL> select instr('yuechaotianyuechao','ao', 7) position from dual;

  POSITION
----------
        17

--2.从第1个字符开始,搜索第2次出现子串的位置
SQL> select instr('yuechaotianyuechao','ao', 1, 2) position from dual;

  POSITION
----------
        17

--3.从倒数第1个字符开始,搜索第1次出现子串的位置
SQL> select instr('yuechaotianyuechao','ao', -1, 1) position from dual;

  POSITION
----------
        17

--3.从倒数第1个字符开始,搜索第2次出现子串的位置
SQL> select instr('yuechaotianyuechao','ao', -1, 2) position from dual;

  POSITION
----------
         6