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 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) )
3) Example screenshot:
1、
2、
5、
6、
7、
8、
9、
10、
15、
16、
17、
4) Complete function instance
1 create or replace function get_request_code return varchar2 AS 2 3 -- 函数的作用:自动生成单号 4 v_mca_no mcode_apply.mca_no%TYPE; 5 6 CURSOR get_max_mca_no IS 7 SELECT max(substr(mca_no, 11, 3)) -- 查出的最大单号,截取出最后三位,如:001、002...00n 8 FROM mcode_apply 9 WHERE substr(mca_no, 3, 8) = to_char(sysdate, 'YYYYMMDD'); -- 截取单号【如:20170422】,to_char():把时间转换为字符型,即string类型。 10 11 v_requestcode VARCHAR2(3); 12 13 BEGIN 14 OPEN get_max_mca_no; 15 FETCH get_max_mca_no INTO v_requestcode; 16 CLOSE get_max_mca_no; 17 18 IF v_requestcode IS NULL THEN 19 v_requestcode := NVL(v_requestcode, 0); -- NVL()函数:当v_requestcode为NULL时,取0作为值 20 END IF; 21 22 v_requestcode := lpad(v_requestcode + 1, 3, '0'); -- 将游标中截取到的值加1,然后向左填充0,生成 001,002...00n 三位数的 序号; lpad()函数:向左填充 23 v_mca_no := 'MA' || to_char(sysdate, 'YYYYMMDD') || v_requestcode; -- 最终生成的申请单号(如:MA20170422001;MA20170422002;...MA2017042200N ) 24 25 RETURN '0~,'|| v_mca_no; 26 27 END ;
注:如要测试该函数,请复制到oracle数据库中,右击函数名 “get_request_code” 选择test测试,测试时记得把相应的表名及字段换成自己建立的
转载自:https://www.cnblogs.com/dshore123/p/7805050.html