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

select instr('abcd','a') from dual;  --返回1
select instr('abcd','c') from dual;  --返回3
select instr('abcd','e') from dual;    --返回0

该函数可以用于模糊查询以及判断包含关系:

例如:

① select code, name, dept, occupation  from staff  where instr(code, '001') > 0;

  等同于

  select code, name, dept, occupation  from staff  where code like '%001%' ;

② select ccn,mas_loc from mas_loc where instr('FH,FHH,FHM',ccn)>0;

  等同于

  select ccn,mas_loc from mas_loc where ccn in ('FH','FHH','FHM');

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

注: 
instr(title,'oracle‟)>0 相当于like

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

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

POSITION

----------

7

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

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

POSITION

----------

7

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

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

POSITION

----------

7

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

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

POSITION

----------

1

经典例子:

select * from tuser a where instr(','||(select substr(owner,8,length(owner)) from os_currentstep where entry_id=4252300)||',',','||a.id||',')>0;

(select substr(owner,8,length(owner)) from os_currentstep where entry_id=4252300) 结果为字符串:3993,451,1889,1482

如果直接这么写:select * from tuser a where a.id in (select substr(owner,8,length(owner)) from os_currentstep where entry_id=4252300)会报无效数字

25. instr用法的更多相关文章

  1. Oracle instr用法

    1:实现indexOf功能,.从第1个字符开始,搜索第1次出现子串的位置 ,) as i from dual; select instr('oracle','or') as i from dual; ...

  2. 2. instr用法

    跟oracle中的instr用法一样: SQL> select count(*) from t where instr(title,‟oracle‟)>0; COUNT(*) ———- 5 ...

  3. Oracle substr() instr() 用法

    转载:oracle中substr() instr() 用法 substr(字符串,截取开始位置,截取长度) = 返回截取的字符串instr(源字符串,目标字符串,起始字符串,匹配字符串) = 返回要截 ...

  4. oracle中substr() instr() 用法

    --substr(字符串,截取开始位置,截取长度)=返回截取的字 ,) from dual;--返回结果为:m ,) from dual;--返回结果为:m--说明0和1都表示截取的位置为第一个字符 ...

  5. Oracle中判断(case when),截取(substr),位置(instr)用法

    转自:http://rainbowdesert.iteye.com/blog/1677911 博客分类: SQL   1.  判断(case when) SELECT col1, col2, CASE ...

  6. Oracle中Instr用法

    在项目中用到了Oracle中 Instr 这个函数,顺便仔细的再次学习了一下这个知识. Oracle中,可以使用 Instr 函数对某个字符串进行判断,判断其是否含有指定的字符. 其语法为:Instr ...

  7. Oracle中INSTR、SUBSTR和NVL的用法

    Oracle中INSTR.SUBSTR和NVL的用法 INSTR用法:INSTR(源字符串, 要查找的字符串, 从第几个字符开始, 要找到第几个匹配的序号) 返回找到的位置,如果找不到则返回0. 默认 ...

  8. oracle中substr与instr

    在oracle中,可以使用instr函数对某个字符串进行判断,判断其是否含有指定的字符.在一个字符串中查找指定的字符,返回被查找到的指定字符的位置. 语法: Instr(sourceString,de ...

  9. angularjs不同页面间controller传参方式,使用service封装sessionStorage

    这里分享一个我在实际项目中,使用service封装的一个依赖sessionStorage的传参服务. 这里先说下大背景,在我们的实际开发中,登陆之后一般会存在一个token,这个token将会贯穿全场 ...

随机推荐

  1. k8s dockerk个人学习(1)

    虚拟机部署k8s 1. 创建虚拟机 虚拟机用的是virtualBox和vagrant工具,百度安装virtualBox和vagrant 创建vagrant目录并创建文件Vagrantfile内容为 V ...

  2. 手把手图文教你eclipse下如何配置tomcat

    很多初学,尤其自学JavaWeb的朋友首次在eclipse下配置tomcat时,总会有种难下手的感觉,在此,笔者通过图文解说的方法,最直观的向大家演示一遍该配置过程,希望对大家有所帮助. 注:本文是建 ...

  3. SELENIUM如何调用FIREFOX时加载插件

    当selenium调用firefox时,会发现这个firefox里干净的如同一盆清水,自己定制安装的那些插件都不翼而飞了,这个时候那些插件自然就不能使用了,但是当前又必须使用插件该如何是好呢? 解决办 ...

  4. H.264帧结构详解

    6.1.2.源码简单浏览 6.1.3.重点1:h.264帧结构6.1.4.重点2:帧结构分析软件的使用6.1.5.重点3:rtsp网络编程6.1.6.重点4:wireshark网络抓包工具的使用 6. ...

  5. 注意字符串的strlen与sizeof的差别

    unsigned char AT_RESET[]="r\r\n"; printf("strlen=%d sizeof=%d\n",strlen(AT_RESET ...

  6. 查看 linux cpu 、内存、服务器型号和序列号、磁盘、raid 的信息

    yum -y install dmidecode 查看cpu的型号: 查看cpu的颗数:dmidecode -t processor |grep "Version"dmidecod ...

  7. valgrind的使用--检测内存

    valgrind主要检测内存的使用情况,检测有否内存泄露等. 比如:test_va2.cpp #include<iostream> using namespace std; int mai ...

  8. 创建mysql 用户并限定其操作主机 和 数据库

    参考链接 http://www.cnblogs.com/top5/archive/2010/09/14/1825571.html ******** GRANT ALL ON push.* TO pus ...

  9. php 内容插入数据库需要mysql_escape_string处理一下 展示内容时候用htmlentities

    php 内容插入数据库需要mysql_escape_string处理一下 mysql_escape_string (PHP 4 >= 4.0.3, PHP 5, 注意:在PHP5.3中已经弃用这 ...

  10. vue 操作数组,原数组怎么不让它改变

    vue中肯定遇到过这个问题,想对数组操作,可是原数组又会改变,怎么办呢,提前copy一份能行吗, todo: [1,2,3,4,5], var arr = this.todo: 这样肯定不行的,那么肯 ...