Oracle sql 模式排序



>我有一个这样的模式 - "pqrst ##ind###123456##ind## hggff 1234"。我必须提取##ind##和##ind##之间的数字。使用 pl/sql。建议

SQL

WITH d
     AS (SELECT 'pqrst ##ind##123456##ind## hggff 1234'       expr,
                '.*[#]{2}ind[#]{2}(d+)[#]{2}ind[#]{2}.*' match
           FROM DUAL)
SELECT expr, REGEXP_REPLACE (expr, match, '1') n
  FROM d
/

PL/SQL

DECLARE
   expr    VARCHAR2 (64) := 'pqrst ##ind##123456##ind## hggff 1234';
   match   VARCHAR2 (64) := '.*[#]{2}ind[#]{2}(d+)[#]{2}ind[#]{2}.*';
BEGIN
   DBMS_OUTPUT.PUT_LINE (REGEXP_REPLACE (expr, match, '1'));
END;
/
SET ESCAPE OFF
DECLARE
    matches VARCHAR2(100) := 'pqrst ##ind##123456##ind## hggff 1234';
    mismatches VARCHAR2(100) := 'pqrst ##id##123456##ind## hggff 1234';
BEGIN
    DBMS_OUTPUT.PUT_LINE('matches:' || REGEXP_REPLACE(matches
            ,'(.*##ind##(.*)##ind##.*)?.*', '2'));
    DBMS_OUTPUT.PUT_LINE('mismatches:' || REGEXP_REPLACE(mismatches
            ,'(.*##ind##(.*)##ind##.*)?.*', '2'));
END;
/
matches:123456
mismatches:
PL/SQL procedure successfully completed.

试试这个:

select  to_number(trim(
            replace(  
                    replace(
                            'pqrst ##ind##123456##ind## hggff 1234', 
                            SUBSTR('pqrst ##ind##123456##ind## hggff 1234', 1, INSTR('pqrst ##ind##123456##ind## hggff 1234', '#',1,4)), -- <-- 4th occurrence of "#"
                            null), 
                    SUBSTR('pqrst ##ind##123456##ind## hggff 1234', INSTR('pqrst ##ind##123456##ind## hggff 1234', '#',1,5), -- <-- 5th occurrence of "#"
                    length('pqrst ##ind##123456##ind## hggff 1234')), 
                    null)
        )) as result
from dual;

通过识别第 4 次和第 5 次出现的"#"并从它们之间提取字符串来工作。

最新更新