ITPub博客

首页 > 数据库 > Oracle > oracle instr()函数

oracle instr()函数

Oracle 作者:534058389 时间:2016-08-08 16:17:33 0 删除 编辑
参考 http://blog.csdn.net/hzhsan/article/details/9186637

oracle instr函数

从一个字符串中查找指定子串的位置。例如:

SQL> select instr('oracle','or') position from dual;

POSITION

----------

        1

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


instr共有4个参数,格式为“instr(string, substring, startposition, occurrence)”
。
可实现子串的如下搜索:

1.从指定位置开始搜索子串

2.指定搜索第几次出现的子串的位置

3.从后向前搜索

一,.从第3个字符开始搜索

SQL> select instr('oracleor','or', 3) position from dual;

POSITION

----------

        7

二,.从第1个字符开始,搜索第2次出现子串的位置

SQL> select instr('oracleor','or', 1, 2) position from dual;

POSITION

----------

        7

三,.从倒数第1个字符开始,搜索第1次出现子串的位置

SQL> select instr('oracleor','or', -1, 1) position from dual;

POSITION

----------

        7

补充一个例子,.从倒数第1个字符开始,自右向左搜索第3次出现子串的位置

SQL> select instr('abcdabcdabcd','d',-1,3) from dual;
 
INSTR('ABCDABCDABCD','D',-1,3)
------------------------------
                             4


oracle用instr代替like

表中将近有100万数据,很多时候,我们要进行字符串匹配,在SQL语句中,我们通常使用like来达到我们搜索的目标。
但经过实际测试发现,like的效率与instr函数差别相当大。下面是一些测试结果:

SQL> set timing on
SQL> select count(*) from t where instr(title,’oracle’)>0;

COUNT(*)
———-
5478

Elapsed: 00:00:11.04
SQL> select count(*) from t where title like ‘%oracle%’;

COUNT(*)
———-
5478

Elapsed: 00:00:31.47
SQL> select count(*) from t where instr(title,’oracle’)=0;

COUNT(*)
———-
994530

Elapsed: 00:00:11.31
SQL> select count(*) from t where title not like ‘%oracle%’;

COUNT(*)
———-
994530

注:

instr(title,'oracle’)>0 相当于like

instr(title,'oracle’)=0 相当于not like

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29074224/viewspace-2123159/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2016-01-11

  • 博文量
    23
  • 访问量
    46685