Detailed case of substr() function in Oracle

1) substr function format (commonly known as: character interception function)

  Format 1: substr(string string, int a, int b);

  Format 2: substr(string string, int a) ;

explain:

Format 1

1. string The string to be intercepted 
2. a is the start position of the intercepted string (Note: when a is equal to 0 or 1, it is intercepted from the first bit)
3. b The length

format of the string to be intercepted 2

1. string String that needs to be intercepted
2. a can be understood as intercepting all subsequent strings starting from the a-th character.

 

2) Instance analysis

1. substr('HelloWorld',0,3); //Return result: Hel, intercept 3 characters starting from "H" 
2. substr('HelloWorld',1,3); //Return result: Hel, intercept 3 characters starting from "H" 
3, substr('HelloWorld',2,3); //Return result: ell, intercept 3 characters starting from "e"
4, substr('HelloWorld',0,100); // Return result: HelloWorld, although 100 exceeds the maximum length of the preprocessed string, it will not affect the returned result. The system returns the maximum number of preprocessed strings.
5. substr('HelloWorld',5,3); //Return result: oWo 
6. substr('Hello World',5,3); //Return result: o W (the space in the middle is also a string, The result is: ospace W )
7. substr('HelloWorld',-1,3); //Return result: d (take 1 character from the bottom of the back, not 3. Reason: the following The third annotation in red)
8. substr('HelloWorld',-2,3); //Return result: ld (Take 2 characters from the second to last place, not 3. Reason: red below The third note)
9. substr('HelloWorld',-3,3); //Return result: rld (take 3 characters from the third to last place)
10.substr('HelloWorld',-4,3); //Return result: orl (take 3 characters from the fourth to last place)

( Note: When a is equal to 0 or 1, it is intercepted from the first digit (eg: 1 and 2) )
( Note: If there is a space between HelloWorld, the space will also be counted (eg: 5 and 6) ) )
( Note: Although 7, 8, 9, and 10 all intercept 3 characters, the result is not 3 characters; as long as |a| ≤ b, take the number of a (eg: 7, 8, 9); When |a| ≥ b, the number of b is taken, and the interception position is determined by a (eg: 9 and 10) )


11. substr('HelloWorld',0); //Return result: HelloWorld, intercept all characters
12, substr('HelloWorld',1); //Return result: HelloWorld, intercept all characters
13, substr('HelloWorld', 2); //Return result: elloWorld, intercept all characters starting from "e"
14, substr('HelloWorld',3); //Return result: lloWorld, intercept all characters starting from "l"
15, substr( 'HelloWorld',-1); //Return result: d, intercept 1 character back from the last "d"
16, substr('HelloWorld',-2); //Return result: ld, from the last "d" starts to intercept 2 characters back 
17, substr('HelloWorld',-3); //Return result: rld, intercept 3 characters back from the last "d"


( Note: When there are only two parameters; no matter the negative number, it will be intercepted from the last one (eg: 15, 16, 17) )

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325692630&siteId=291194637