您的位置:首页 > 数据库 > Oracle

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');
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  oracle 存储过程