oracle截取字符串

substr函数格式   (俗称:字符截取函数)

格式1: substr(string string, int a, int b);  
格式2:substr(string string, int a) ;

解释:

格式1
1、string 需要截取的字符串 
2、a 截取字符串的开始位置(注:当a等于0或1时,都是从第一位开始截取)
3、b 要截取的字符串的长度
格式2
1、string 需要截取的字符串
2、a 可以理解为从第a个字符开始截取后面所有的字符串。

实例解析

1、substr('HelloWorld',0,3); //返回结果:Hel,截取从“H”开始3个字符

2、substr('HelloWorld',1,3); //返回结果:Hel,截取从“H”开始3个字符

3、substr('HelloWorld',2,3); //返回结果:ell,截取从“e”开始3个字符

4、substr('HelloWorld',0,100); //返回结果:HelloWorld,100虽然超出预处理的字符串最长度,但不会影响返回结果,系统按预处理字符串最大数量返回。

5、substr('HelloWorld',5,3); //返回结果:oWo

6、substr('Hello World',5,3); //返回结果:o W (中间的空格也算一个字符串,结果是:o空格W)

7、substr('HelloWorld',-1,3); //返回结果:d (从后面倒数第一位开始往后取1个字符,而不是3个。原因:下面红色 第三个注解)

8、substr('HelloWorld',-2,3); //返回结果:ld (从后面倒数第二位开始往后取2个字符,而不是3个。原因:下面红色 第三个注解)

9、substr('HelloWorld',-3,3); //返回结果:rld (从后面倒数第三位开始往后取3个字符)

10、substr('HelloWorld',-4,3); //返回结果:orl (从后面倒数第四位开始往后取3个字符)(注:当a等于0或1时,都是从第一位开始截取(如:1和2))
(注:假如HelloWorld之间有空格,那么空格也将算在里面(如:5和6))
(注:虽然7、8、9、10截取的都是3个字符,结果却不是3 个字符; 只要 |a| ≤ b,取a的个数(如:7、8、9);当 |a| ≥ b时,才取b的个数,由a决定截取位置(如:9和10))

11、substr('HelloWorld',0);  //返回结果:HelloWorld,截取所有字符

12、substr('HelloWorld',1);  //返回结果:HelloWorld,截取所有字符

13、substr('HelloWorld',2);  //返回结果:elloWorld,截取从“e”开始之后所有字符

14、substr('HelloWorld',3);  //返回结果:lloWorld,截取从“l”开始之后所有字符

15、substr('HelloWorld',-1);  //返回结果:d,从最后一个“d”开始 往回截取1个字符

16、substr('HelloWorld',-2);  //返回结果:ld,从最后一个“d”开始 往回截取2个字符

17、substr('HelloWorld',-3);  //返回结果:rld,从最后一个“d”开始 往回截取3个字符
(注:当只有两个参数时;不管是负几,都是从最后一个开始 往回截取(如:15、16、17))

原文地址:https://www.cnblogs.com/jichi/p/11139533.html