I want to get decimal value of the given ipaddress in SQL
example 192.168.0.0
its decimal representation would be 3232235520
. IP - DECIMAL
select
TO_NUMBER(LISTAGG(binary1, '') WITHIN GROUP ( ORDER BY cnt), 'XXXXXXXX') as Numeric1
from (
select 1 as grp, rownum as cnt
, NVL(TRIM(LEADING '0' FROM utl_raw.cast_from_binary_integer(regexp_substr('192.168.255.255','\d+', 1, rownum))), '00') as binary1
from dual connect by rownum <= 4
) temp
group by grp
I am using oracle 11g and I have come up with the above approach.
- convert the octet into binary representation using
ult_raw.cast_from_binary_integer
but internally binary gets converted to hex representation. Therefore the output of the inner query is basically hex representation of the octet. - Hex representation is obtained by clubbing 4 binarys example (1000)2 = (8)16 therefore getting decimal value of (10001000)2 is equal to getting the value for (88)16
- I have appended the hex representation preserving the order of octet with
cnt
column and clubbing the result withLISTAGG
UPDATE
I am using LISTAGG
and connect by clauses
does this have any performance issues when I try to convert 1000s of records. this method as suggested is simple and readable but there is no mathematical explanation. second method this uses PL/SQL functions, i will look at this as my last resort.