Oracle中INSTR的用法:
INSTR方法的格式為
INSTR(源字符串, 要查找的字符串, 從第幾個(gè)字符開始, 要找到第幾個(gè)匹配的序號(hào))
返回找到的位置,如果找不到則返回0.
例如:INSTR('CORPORATE FLOOR','OR', 3, 2)
中,源字符串為'CORPORATE FLOOR'
, 在字符串中查找'OR',從第三個(gè)字符位置開始查找"OR",取第三個(gè)字后第2個(gè)匹配項(xiàng)的位置。
默認(rèn)查找順序?yàn)閺淖蟮接?。?dāng)起始位置為負(fù)數(shù)的時(shí)候,從右邊開始查找。
所以SELECT INSTR('CORPORATE FLOOR', 'OR', -1, 1) "aaa" FROM DUAL
的顯示結(jié)果是
Instring
——————
14
oracle的substr函數(shù)的用法:
取得字符串中指定起始位置和長(zhǎng)度的字符串 substr( string, start_position, [ length ] )
如:
substr('This is a test', 6, 2) would return 'is'
substr('This is a test', 6) would return 'is a test'
substr('TechOnTheNet', -3, 3) would return 'Net'
substr('TechOnTheNet', -6, 3) would return 'The'
select substr('Thisisatest', -4, 2) value from dual
綜合應(yīng)用:
SELECT INSTR('CORPORATE FLOOR', 'OR', -1, 1) "Instring" FROM DUAL
--INSTR(源字符串, 目標(biāo)字符串, 起始位置, 匹配序號(hào))
SELECT INSTR('CORPORATE FLOOR','OR', 3, 2) "Instring" FROM DUAL
SELECT INSTR('32.8,63.5',',', 1, 1) "Instring" FROM DUAL
SELECT SUBSTR('32.8,63.5',INSTR('32.8,63.5',',', 1, 1)+1) "INSTRING" FROM DUAL
SELECT SUBSTR('32.8,63.5',1,INSTR('32.8,63.5',',', 1, 1)-1) "INSTRING" FROM DUAL
-- CREATED ON 2008-9-26 BY ADMINISTRATOR
DECLARE
-- LOCAL VARIABLES HERE
T VARCHAR2(2000);
S VARCHAR2(2000);
NUM INTEGER;
I INTEGER;
POS INTEGER;
BEGIN
-- TEST STATEMENTS HERE
T := '12.3,23.0;45.6,54.2;32.8,63.5;';
SELECT LENGTH(T) - LENGTH(REPLACE(T, ';', '')) INTO NUM FROM DUAL;
DBMS_OUTPUT.PUT_LINE('NUM:' || NUM);
POS := 0;
FOR I IN 1 .. NUM LOOP
DBMS_OUTPUT.PUT_LINE('I:' || I);
DBMS_OUTPUT.PUT_LINE('POS:' || POS);
DBMS_OUTPUT.PUT_LINE('==:' || INSTR(T, ';', 1, I));
DBMS_OUTPUT.PUT_LINE('INSTR:' || SUBSTR(T, POS + 1, INSTR(T, ';', 1, I) - 1));
POS := INSTR(T, ';', 1, I);
END LOOP;
END;
-- Created on 2008-9-26 by ADMINISTRATOR
declare
-- Local variables here
i integer;
T VARCHAR2(2000);
S VARCHAR2(2000);
begin
-- Test statements here
--歷史狀態(tài)
T := '12.3,23.0;45.6,54.2;32.8,63.5;';
IF (T IS NOT NULL) AND (LENGTH(T) > 0) THEN
--T := T || ',';
WHILE LENGTH(T) > 0 LOOP
--ISTATUSID := 0;
S := TRIM(SUBSTR(T, 1, INSTR(T, ';') - 1));
IF LENGTH(S) > 0 THEN
DBMS_OUTPUT.PUT_LINE('LAT:'||SUBSTR('32.8,63.5',1,INSTR('32.8,63.5',',', 1, 1)-1));
DBMS_OUTPUT.PUT_LINE('LON:'||SUBSTR('32.8,63.5',INSTR('32.8,63.5',',', 1, 1)+1));
-- COMMIT;
END IF;
T := SUBSTR(T, INSTR(T, ';') + 1);
END LOOP;
END IF;
end;
PS:下面看下Oracle中INSTR、SUBSTR和NVL的用法
INSTR用法:INSTR(源字符串, 要查找的字符串, 從第幾個(gè)字符開始, 要找到第幾個(gè)匹配的序號(hào))
返回找到的位置,如果找不到則返回0. 默認(rèn)查找順序?yàn)閺淖蟮接?。?dāng)起始位置為負(fù)數(shù)的時(shí)候,從右邊開始查找。若起始位置為0,返回值為0。
SELECT INSTR('CORPORATE FLOOR', 'OR', 0, 1) FROM DUAL; 返回值為0
SELECT INSTR('CORPORATE FLOOR', 'OR', 2, 1) FROM DUAL; 返回值為2
SELECT INSTR('CORPORATE FLOOR', 'OR', 2, 2) FROM DUAL; 返回值為5
SELECT INSTR('CORPORATE FLOOR', 'OR', -1, 1) FROM DUAL; 返回值為14
SELECT INSTR('CORPORATE FLOOR', 'OR', -5, 1) FROM DUAL; 返回值為5
SUBSTR用法:SUBSTR( 源字符串, 查找起始位置, [ 長(zhǎng)度 ] )
返回值為源字符串中指定起始位置和長(zhǎng)度的字符串。
SELECT SUBSTR('This is a test', 0, 2) value from dual; 返回值Th
SELECT SUBSTR('This is a test', 1, 2) value from dual; 返回值Hi
SELECT SUBSTR('This is a test', -1, 2) value from dual; 返回值t
SELECT SUBSTR('This is a test', -2, 2) value from dual; 返回值st
NVL用法:NVL(eExpression1, eExpression2)
從兩個(gè)表達(dá)式返回一個(gè)非 null 值。如果eExpression1的計(jì)算結(jié)果為null值,則 NVL( ) 返回eExpression2。如果eExpression1的計(jì)算結(jié)果不是null值,則返回eExpression1。eExpression1 和eExpression2可以是任意一種數(shù)據(jù)類型。如果eExpression1與eExpression2 的結(jié)果皆為 null值,則NVL( )返回NULL。
SELECT nvl('pos1',null) from dual; 返回值為pos1
SELECT nvl(null,'pos2') from dual; 返回值為pos1
SELECT nvl(null,null) from dual; 返回值為null
以上所述是小編給大家介紹的Oracle中的INSTR,NVL和SUBSTR函數(shù)的用法詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
您可能感興趣的文章:- oracle數(shù)據(jù)庫(kù)中sql%notfound的用法詳解
- Oracle minus用法詳解及應(yīng)用實(shí)例
- oracle數(shù)據(jù)庫(kù)定時(shí)任務(wù)dbms_job的用法詳解
- Oracle存儲(chǔ)過程游標(biāo)用法分析
- Oracle開發(fā)之分析函數(shù)簡(jiǎn)介Over用法
- 講解Oracle數(shù)據(jù)庫(kù)中的數(shù)據(jù)字典及相關(guān)SQL查詢用法
- Oracle中游標(biāo)Cursor基本用法詳解