很多时候,我们要进行字符串匹配,在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. (4)对象的的初始化与__init__方法以及绑定方法

    class OldboyStudent: # name='xxxx' # 相似的特征: school = 'oldboy' # 相似的技能 def choose_course(self): print ...

  2. k8s dockerk个人学习(1)

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

  3. day40 python MySQL【四】 之 【索引】【视图】【触发器】【存储过程】【函数】

    MySQL[四] 之 [索引][视图][触发器][存储过程][函数]   1.索引 索引相当于图书的目录,可以帮助用户快速的找到需要的内容. 数据库利用各种各样的快速定位技术,能够大大提高查询效率.特 ...

  4. linux的性能优化

    转一位大神的笔记. linux的性能优化: 1.CPU,MEM 2.DISK--RAID 3.网络相关的外设,网卡 linux系统性能分析: top:linux系统的负载,CPU,MEM,SWAP,占 ...

  5. hashcode 知识点

    java中常用的哈希码的算法. Object类的hashCode.返回对象的内存地址经过处理后的结构,由于每个对象的内存地址都不一样,所以哈希码也不一样. String类的hashCode.根据Str ...

  6. Dynamics CRM 2011 快速查找 出现异常 QuickFindQueryRecordLimit exceeded. Cannot perform this operation 的解决方法

    一.CRM 2011 快速查找,输入编号的签名几个字母发现查询很慢. 图 1 当然在图1 上右边的出入框输入编号的部分的时候,有时候会发现数据在加载中..,非常慢,通过Crm Trace Log Vi ...

  7. json包含单双引号问题解决方案

    解决方案:在后台处理 JSONArray.fromObject(list).toString() 转自明明如月小角落: 效果DEMO: JsonQuotesUtil.js /** * 解决json传输 ...

  8. nginx在centos & ubuntu上的安装

    安装Centos 添加当前账号加入sudoers,具备sudo功能 安装编辑器vim Yum install vim Su root Cd cp /etc/sudoers /etc/sudoers[d ...

  9. 在doc下面执行zpl语言 Copy 文件名 lpt1

    在doc下面执行zpl语言 Copy 文件名 lpt1 测试斑马条码打印机并口是否能正常通讯: 1.Zebra打印机安装好标签和碳带,且打印机进入Ready状态.2.电脑端用Notepad编写输入如下 ...

  10. python findall() re.S

    官方文档:https://docs.python.org/3.6/library/re.html 教程:http://www.regexlab.com/zh/regref.htm re.findall ...