25. instr用法
很多时候,我们要进行字符串匹配,在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用法的更多相关文章
- Oracle instr用法
1:实现indexOf功能,.从第1个字符开始,搜索第1次出现子串的位置 ,) as i from dual; select instr('oracle','or') as i from dual; ...
- 2. instr用法
跟oracle中的instr用法一样: SQL> select count(*) from t where instr(title,‟oracle‟)>0; COUNT(*) ———- 5 ...
- Oracle substr() instr() 用法
转载:oracle中substr() instr() 用法 substr(字符串,截取开始位置,截取长度) = 返回截取的字符串instr(源字符串,目标字符串,起始字符串,匹配字符串) = 返回要截 ...
- oracle中substr() instr() 用法
--substr(字符串,截取开始位置,截取长度)=返回截取的字 ,) from dual;--返回结果为:m ,) from dual;--返回结果为:m--说明0和1都表示截取的位置为第一个字符 ...
- Oracle中判断(case when),截取(substr),位置(instr)用法
转自:http://rainbowdesert.iteye.com/blog/1677911 博客分类: SQL 1. 判断(case when) SELECT col1, col2, CASE ...
- Oracle中Instr用法
在项目中用到了Oracle中 Instr 这个函数,顺便仔细的再次学习了一下这个知识. Oracle中,可以使用 Instr 函数对某个字符串进行判断,判断其是否含有指定的字符. 其语法为:Instr ...
- Oracle中INSTR、SUBSTR和NVL的用法
Oracle中INSTR.SUBSTR和NVL的用法 INSTR用法:INSTR(源字符串, 要查找的字符串, 从第几个字符开始, 要找到第几个匹配的序号) 返回找到的位置,如果找不到则返回0. 默认 ...
- oracle中substr与instr
在oracle中,可以使用instr函数对某个字符串进行判断,判断其是否含有指定的字符.在一个字符串中查找指定的字符,返回被查找到的指定字符的位置. 语法: Instr(sourceString,de ...
- angularjs不同页面间controller传参方式,使用service封装sessionStorage
这里分享一个我在实际项目中,使用service封装的一个依赖sessionStorage的传参服务. 这里先说下大背景,在我们的实际开发中,登陆之后一般会存在一个token,这个token将会贯穿全场 ...
随机推荐
- Word所有字体按比例缩小
ctrl + [ 不然每次都要一部分一部分的修改啊
- SPOJCOT2 Count on a tree II
分析 树上莫队裸题. 好博客 树剖的时候不能再次dfs重儿子.(好像是废话,但我因为这个问题调了三小时) 代码 #include<cstdlib> #include<cstdio&g ...
- XSL自定义函数
利用微软的XSL的继承特性,用户可以自定义XSL函数.基本原理是用户写的脚本代码写在msxsl中,并设置这部分msxsl继承到用户自定义空间中,那么用户就可以通过用户自定义空间使用msxsl中的脚本代 ...
- Linux下的Nginx部署禅道
基本思路:先安装好nginx和mysql和php,上传禅道的源码.把禅道的源码包扔到 nginx/apache 的工程路径内或者nginx/apache内的配置文件指向nginx的路径,然后将ngin ...
- centos6/7安装gitlab
CentOS/RHEL 6/7安装gitlab新建 /etc/yum.repos.d/gitlab-ce.repo,内容为你的CentOS/RHEL版本:centos6 [gitlab-ce] nam ...
- hanlp中文自然语言处理的几种分词方法
自然语言处理在大数据以及近年来大火的人工智能方面都有着非同寻常的意义.那么,什么是自然语言处理呢?在没有接触到大数据这方面的时候,也只是以前在学习计算机方面知识时听说过自然语言处理.书本上对于自然语言 ...
- Debug---Eclipse断点调试基础
1.进入debug模式(基础知识列表)1.设置断点 2.启动servers端的debug模式 3.运行程序,在后台遇到断点时,进入debug调试状态 ========================= ...
- linux sh文件提示 no such file or directory
Linux执行.sh文件,提示No such file or directory的问题的解决方法 12-06-28 16:59作者:love__coder Linux执行.sh文件,提示No such ...
- JavaScript模块化-RequireJs实现AMD规范的简单例子
AMD规范简介 AMD(异步模块定义),是实现JavaScript模块化规范之一,它采用异步方式加载模块,模块的加载不影响后面语句的运行.require.js和curl.js都是实现AMD规范的优秀加 ...
- MySQL数据库函数
一:字符串函数: 1.concat(); concat(S1,S2,S3,......Sn); 把传入参数链接 成一个字符串; 2.insert(); insert(str,x,y,insert); ...