如何在1列内对IPv4和IPv6地址进行排序

时间:2017-06-12 11:00:41

标签: sql oracle ipv6 ipv4

IPCIDR                           FLAG_V6
-------------------------------- ----------
58.97.1.12/30                   
58.97.1.16/30                   
58.97.1.96/30                   
58.97.1.100/30                  
58.97.1.116/30                  
2001:fb0:1061:99:0:0:0:0 /64      1 
2001:fb0:1061:9a:0:0:0:0 /64      1 
2001:fb0:1061:9f:0:0:0:0 /64      1 
2001:fb0:1061:a2:0:0:0:0 /64      1 

我想排序ipv4

 order by (to_number(substr(ipcidr,1,instr(ipcidr,'.')-1))
 , to_number(substr(ipcidr,instr(ipcidr,'.')+1, instr(ipcidr,'.',1,2) - instr(ipcidr,'.') - 1))
 , to_number(substr(ipcidr,instr(ipcidr,'.',1,2)+1, instr(ipcidr,'.',1,3) - instr(ipcidr,'.',1,2) - 1))
 , to_number(substr(ipcidr,instr(ipcidr,'.',1,3)+1,instr(ipcidr,'/')-instr(ipcidr,'.',1,3) - 1))
 , to_number(substr(ipcidr,instr(ipcidr,'/')+1)))

并在一个列中排序ipv6,如果ipv6设置为1,则我有FLAG_V6列

1 个答案:

答案 0 :(得分:1)

您可以使用此功能将IP地址转换为整数:

-- Convert a binary/octal/hex number into a decimal value 
CREATE FUNCTION Base2Dec(BaseString IN VARCHAR2, Base IN PLS_INTEGER DEFAULT 16) RETURN NUMBER DETERMINISTIC IS

    BaseNumber NUMBER := 0;
    HexString CONSTANT CHAR(16) := '0123456789ABCDEF';

BEGIN
    IF BaseString IS NULL THEN
        RETURN NULL;
    ELSIF Base NOT IN (2, 8, 16) THEN 
        RAISE VALUE_ERROR;
    ELSIF Base = 16 THEN
        RETURN TO_NUMBER(BaseString, 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
    ELSE
        FOR i IN 1..LENGTH(BaseString) LOOP
            BaseNumber := BaseNumber * Base + INSTR(HexString, UPPER(SUBSTR(BaseString, i, 1))) - 1;
        END LOOP;
        RETURN BaseNumber;
    END IF; 
END Base2Dec;

CREATE FUNCTION UncompressIpV6(Ip IN VARCHAR2) RETURN VARCHAR2 DETERMINISTIC IS
    IpFull VARCHAR2(40);
BEGIN
    IF REGEXP_LIKE(Ip, '::') THEN
        IpFull := REGEXP_REPLACE(REGEXP_REPLACE(Ip, '^::', '0::'), '::$', '::0');
        WHILE REGEXP_COUNT(IpFull, ':') <= 7 LOOP
            IpFull := REGEXP_REPLACE(IpFull, '::', ':0::');
        END LOOP;   
        RETURN REGEXP_REPLACE(IpFull, '::', ':');
    ELSE
        RETURN Ip;
    END IF;
END UncompressIpV6;

CREATE FUNCTION Ip2Decimal(IP IN VARCHAR2) RETURN NUMBER DETERMINISTIC IS
    DecimalIp NUMBER := 0;
BEGIN

    IF REGEXP_LIKE(IP, ':') THEN
        SELECT SUM(Base2Dec(REGEXP_SUBSTR(UncompressIpV6(IP), '[[:xdigit:]]+', 1, LEVEL), BASE_HEX) * POWER(65536, 8-LEVEL))
        INTO DecimalIp
        FROM dual 
        CONNECT BY LEVEL <= 8;
        RETURN DecimalIp;       
    ELSE
        SELECT SUM(REGEXP_SUBSTR(IP, '\d+', 1, LEVEL) * POWER(256, 4-LEVEL))
        INTO DecimalIp
        FROM dual 
        CONNECT BY LEVEL <= 4;
        RETURN DecimalIp;   
    END IF;

END Ip2Decimal;

适用于IPv4和IPv6。对于IPv4而言,它应该是非常明显的。 IPv6工作。

您可以在ORDER BY子句中使用它:

ORDER BY FLAG_V6 NULLS FIRST, Ip2Decimal(IPCIDR)