很多时候,我们要进行字符串匹配,在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. Linux内核时间

    1. printk打印的时间戳 [ 7236.595796] usb 2-1.1: new high-speed USB device number 15 using ehci-platform st ...

  2. c++从string类型转换为bool类型

    利用输入字符串流istringstream bool b; string s="true"; istringstream(s)>>boolalpha>>b; ...

  3. java中使用MD5加密的算法

    MD5,全名Message Digest Algorithm 5,中文名为消息摘要算法第五版,为计算机安全领域广泛使用的一种散列函数,用以提供消息的完整性保护.以下是JAVA语言中使用MD5加密的工具 ...

  4. SQLServer2008开启远程连接

    1.查看sqlserver brower协议是否启动 2.对象资源管理器 右键属性->选择-> 方面->服务器配置->Remoteaccess ->True 3.对象资源 ...

  5. bootstrap+font-awesome表单

    bootstrap+font-awesome表单 <form action="" class="form-horizontal col-sm-offset-4&qu ...

  6. 实际用户ID和有效用户ID (一) *****

    在Unix进程中涉及多个用户ID和用户组ID,包括如下: 1.实际用户ID和实际用户组ID:标识我是谁.也就是登录用户的uid和gid,比如我的Linux以simon登录,在Linux运行的所有的命令 ...

  7. samba 挂载windows共享文件夹

    先转载一片文章     centOS下yum安装配置samba 地址 http://blog.csdn.net/linglongwunv/article/details/5212875 遇到问题1 # ...

  8. 【ActiveMQ入门-4】ActiveMQ学习-异步接收

    总体说明: 1. 一个生产者/发布者:可以向多个目的地发送消息: 2. 每个目的地(destination)可以有多个订阅者或消费者: 如下图所示: 程序结构: 1. Publisher.java   ...

  9. ASP.NET Web Pages:对象

    ylbtech-.Net-ASP.NET Web Pages:对象 1.返回顶部 1. ASP.NET Web Pages - 对象 Web Pages 经常是跟对象有关的. Page 对象 您已经看 ...

  10. 正则的使用及replace细讲

    1.var reg=/./ 与 var reg=/\./的区别? .代表任意一个字符 \.而后者代表这个字符串中得有一个. 2.?的使用 如果单独的一个字符后面带? /\d?/ 代表1个或0个这个字符 ...