2017-02-16 68 views
-1

在Oracle我想2串比較如下REGEXP_REPLACE字符串比較

19.12.XX.11 = 19.12.YY.11

我已經與只嘗試下面的一個,但它替換第一個字符只有

select REGEXP_REPLACE('19.12.1.11' ,'.', 'x', 4, 4 ) 
    from dual; 
+2

'在這種情況下LIKE'運營商將做的工作就好了。例如:'在哪裏ipaddr像'19 .12。%。11'' –

+0

@NicholasKrasnov你評論自己的答案 – saikumarm

回答

0

甲骨文設置

CREATE TABLE ipaddresses (addr1, addr2) AS 
    SELECT '19.12.0.11', '19.12.1.11' FROM DUAL; 

查詢

SELECT addr1, 
     addr2, 
     CASE TO_NUMBER(REGEXP_SUBSTR(addr1, '\d+', 1, 1)) 
     WHEN TO_NUMBER(REGEXP_SUBSTR(addr2, '\d+', 1, 1)) 
     THEN 'Match' 
     ELSE 'No Match' 
     END AS Byte1, 
     CASE TO_NUMBER(REGEXP_SUBSTR(addr1, '\d+', 1, 2)) 
     WHEN TO_NUMBER(REGEXP_SUBSTR(addr2, '\d+', 1, 2)) 
     THEN 'Match' 
     ELSE 'No Match' 
     END AS Byte2, 
     CASE TO_NUMBER(REGEXP_SUBSTR(addr1, '\d+', 1, 3)) 
     WHEN TO_NUMBER(REGEXP_SUBSTR(addr2, '\d+', 1, 3)) 
     THEN 'Match' 
     ELSE 'No Match' 
     END AS Byte3, 
     CASE TO_NUMBER(REGEXP_SUBSTR(addr1, '\d+', 1, 4)) 
     WHEN TO_NUMBER(REGEXP_SUBSTR(addr2, '\d+', 1, 4)) 
     THEN 'Match' 
     ELSE 'No Match' 
     END AS Byte4 
FROM ipaddresses; 

輸出

ADDR1  ADDR2  BYTE1 BYTE2 BYTE3 BYTE4 
---------- ---------- -------- -------- -------- -------- 
19.12.0.11 19.12.1.11 Match Match No Match Match