oracle如何实现lastindexof的功能
2015-11-30 09:56
417 查看
第一种实现方法:reverse(substr(reverse(str),1,INSTR(reverse(str),'_') - 1))利用reverse函数,该函数的功能是反转倒置。但是据测试在存储过程中不能使用。于是到网上找了另外一种实现方法。
第二种:substr(str,instr(str,'_',-1,1)+1,length(str));这种实现没那么难懂了。利用了instr函数求得instr(str,'_',-1,1)求得最后一个‘_’的位置(开始位置)所以我们在用substr截取的时候要加1。instr语法:
instr(string1,string2[,start_position[,nth_appearence]])
string1:要在此字符串中查找。
string2:要在string1中查找的字符串。
start_position:从string1开始查找的位置。可选,默认为1,正数时,从左到右检索,负数时,从右到左检索。
nth_appearence:查找第几次出现string2。可选,默认为1,不能为负。
我们这里第三个参数为-1即从右边查找。正好就是最后一个'_'的开始位置。
ps:insrtr可以实现类似like的功能和in的功能(效率问题没有研究)
select name from table where instr(name,
'xx') > 0;等同于select name from table where name like '%xx%';
select name from table where instr('xx,xxx,xxxx',name)>0; 等同于 select name from table where name in('xx','xxx','xxxx');
第二种:substr(str,instr(str,'_',-1,1)+1,length(str));这种实现没那么难懂了。利用了instr函数求得instr(str,'_',-1,1)求得最后一个‘_’的位置(开始位置)所以我们在用substr截取的时候要加1。instr语法:
instr(string1,string2[,start_position[,nth_appearence]])
string1:要在此字符串中查找。
string2:要在string1中查找的字符串。
start_position:从string1开始查找的位置。可选,默认为1,正数时,从左到右检索,负数时,从右到左检索。
nth_appearence:查找第几次出现string2。可选,默认为1,不能为负。
我们这里第三个参数为-1即从右边查找。正好就是最后一个'_'的开始位置。
ps:insrtr可以实现类似like的功能和in的功能(效率问题没有研究)
select name from table where instr(name,
'xx') > 0;等同于select name from table where name like '%xx%';
select name from table where instr('xx,xxx,xxxx',name)>0; 等同于 select name from table where name in('xx','xxx','xxxx');
相关文章推荐
- MySQL存储过程
- 基于 Red Hat 的发行版 Oracle Linux 正式发布Oracle Linux 7.1
- Oracle Containers for J2EE远程安全漏洞(CVE-2014-0413)
- Oracle 10g R2不能使用EM的问题
- 表空间操作
- PreparedStatement中in子句的处理
- VMware下RedHat4.8_64位安装Oracle 10g RAC--简略脚本
- oracle sql日期比较
- 基于 Red Hat 的发行版 Oracle Linux 正式发布Oracle Linux 7.1
- OS block size和Oracle block size,查找OS Blocksize的方法
- oracle中创建数据库和表空间的几点总结
- 数据库自动备份脚本
- ASP程序与SQL存储过程结合使用详解
- oracle的nvl函数的使用介绍
- 解决oracle用户连接失败的解决方法
- oracle的一些tips技巧
- Oracle 下的开发日积月累
- Oracle存储过程之数据库中获取数据实例