Oracle的substr和instr函数简单用法

oracle的substr函数简单用法

substr(字符串,截取开始位置,截取长度) //返回截取的字

substr(‘hello world’,0,1) //返回结果为 ‘h’  *从字符串第一个字符开始截取长度为1的字符串
substr(‘hello world’,1,1) //返回结果为 ‘h’  *0和1都是表示截取的开始位置为第一个字符
substr(‘hello world’,2,4) //返回结果为 ‘ello’
substr(‘hello world’,-3,3)//返回结果为 ‘rld’ *负数(-i)表示截取的开始位置为字符串右端向左数第i个字符 

测试:

select substr('hello world',-3,3) value from dual;

附:java中substring(index1,index2)的简单用法 

作用:从字符串索引(下标)为index1的字符开始截取长度为index2-index1 的字符串。

string str="hello world";
system.out.println(str.substring(0,5));

打印结果为:hello

oracle中instr的用法:

instr方法的格式为

instr(源字符串, 要查找的字符串, 从第几个字符开始, 要找到第几个匹配的序号)
返回找到的位置,如果找不到则返回0.

例如:instr(‘corporate floor’,’or’, 3, 2)中,源字符串为’corporate floor’, 在字符串中查找’or’,从第三个字符位置开始查找”or”,取第三个字后第2个匹配项的位置。

默认查找顺序为从左到右。当起始位置为负数的时候,从右边开始查找。

所以select instr(‘corporate floor’, ‘or’, -1, 1) “aaa” from dual的显示结果是

instring

——————

14

(0)
上一篇 2022年3月22日
下一篇 2022年3月22日

相关推荐