Hard to Get

--人生在世 难得二字

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
splitStr(inputStr IN VARCHAR2, splitBy IN VARCHAR2, returnSerial IN NUMBER) RETURN VARCHAR2

返回 inputStr  被 splitBy  分割后的第 returnSerial  个子字符串

inputStr: 被操作的字符串
splitBy: 用来分割被操作字符串的字符或字符串
returnSerial: 期望返回的子字符串在分割出的数组中的序号
如:
    select splitstr('abcd$efg','$',1) from dual;  得到 'abcd';
    select splitstr('abcd$efg','$',2) from dual;  得到 'efg';
    select splitstr('abcd$efg','$',3) from dual;  得到 null;
    select splitstr('abcd$efg','$',0) from dual;  得到 null;    
    select splitstr('abcd$efg','#',1) from dual;  得到 'abcd$efg';   

Function:
 1CREATE OR REPLACE FUNCTION splitStr(inputStr IN VARCHAR2, splitBy IN VARCHAR2, returnSerial IN NUMBERRETURN VARCHAR2 IS
 2    RESULT VARCHAR2(32767);
 3BEGIN
 4
 5    IF NOT returnSerial < 1 THEN
 6        IF returnSerial = 1 THEN
 7            IF instr(inputStr, splitBy, 11= 0 THEN
 8                RESULT := inputStr;
 9            ELSE
10                RESULT := substr(inputStr, 1, instr(inputStr, splitBy, 11- 1);
11            END IF;
12        ELSE
13            IF instr(inputStr, splitBy, 1, returnSerial - 1= 0 THEN
14                RESULT := NULL;
15            ELSE
16                IF instr(inputStr, splitBy, 1, returnSerial) = 0 THEN
17                    RESULT := substr(inputStr, instr(inputStr, splitBy, 1, returnSerial - 1+ length(splitBy),
18                                                     length(inputStr) - instr(inputStr, splitBy, 1, returnSerial - 1- length(splitBy));
19                ELSE
20                    RESULT := substr(inputStr, instr(inputStr, splitBy, 1, returnSerial - 1+ length(splitBy),
21                                                     instr(inputStr, splitBy, 1, returnSerial) - instr(inputStr, splitBy, 1, returnSerial - 1-
22                                                        length(splitBy));
23                END IF;
24            END IF;
25        END IF;
26    ELSE
27        RESULT := NULL;
28    END IF;
29
30    RETURN(RESULT);
31END splitStr;
posted on 2005-08-30 22:14  Del  阅读(304)  评论(0)    收藏  举报