Oracle中INSTR和SUBSTR的用法

2021-06-23 02:47:09 字數 2632 閱讀 8632

oracle中instr的用法:

instr方法的格式為

instr(源字串, 要查詢的字串, 從第幾個字元開始, 要找到第幾個匹配的序號)

返回找到的位置,如果找不到則返回0.

例如:instr('corporate floor','or', 3, 2)中,源字串為'corporate floor', 在字串中查詢'or',從第三個字元位置開始查詢"or",取第三個字後第2個匹配項的位置。

預設查詢順序為從左到右。當起始位置為負數的時候,從右邊開始查詢。

所以select instr('corporate floor', 'or', -1, 1) "aaa" from dual的顯示結果是

"aaa"

——————14

oracle的substr函式的用法:

取得字串中指定起始位置和長度的字串 

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 顯示結果是

value

_____ te

綜合應用:

select instr('corporate floor', 'or', -1, 1) "instring" from dual

--instr(源字串, 目標字串, 起始位置, 匹配序號)

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

declare

t  varchar2(2000);

s  varchar2(2000);

num integer;

i  integer;

pos integer;

begin

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;

declare

i integer;

t  varchar2(2000);

s  varchar2(2000);

begin

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;

oracle中substr和instr的用法

網上蒐集的,整理下 1 substr string string,int a,int b 引數1 string 要處理的字串 引數2 a 擷取字串的開始位置 起始位置是0 引數3 b 擷取的字串的長度 而不是字串的結束位置 例如 substr abcdefg 0 返回 abcdefg,擷取所有字元 ...

Oracle中INSTR和SUBSTR的用法

oracle中instr和substr的用法 oracle中instr的用法 instr方法的格式為 instr 源字串,要查詢的字串,從第幾個字元開始,要找到第幾個匹配的序號 返回找到的位置,如果找不到則返回0.例如 instr corporate floor or 3,2 中,源字串為 corp...

Oracle中INSTR和SUBSTR的用法

oracle中instr和substr的用法 oracle中instr的用法 instr方法的格式為 instr 源字串,要查詢的字串,從第幾個字元開始,要找到第幾個匹配的序號 返回找到的位置,如果找不到則返回0.例如 instr corporate floor or 3,2 中,源字串為 corp...