Oracle中INSTR和SUBSTR的用法____Oracle

來源:互聯網
上載者:User
Oracle中INSTR和SUBSTR的用法
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的顯示結果是
Instring
——————
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


綜合應用:
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
-- 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 := '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;
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.