Sort by IP Address in Oracle
681263May 21 2009 — edited May 22 2009I have the following SQL Server code that I used to sort by an IP address:
declare @ip table(ip_address varchar(15))
insert into @ip
select '75.158.2.101' union all
select '100.101.102.103'
select
*
from
( select *,
cast(parsename(ip_address, 4) as int) as seg1,
cast(parsename(ip_address, 3) as int) as seg2,
cast(parsename(ip_address, 2) as int) as seg3,
cast(parsename(ip_address, 1) as int) as seg4
from @ip
) x
order by seg1, seg2, seg3, seg4
-- ip_address seg1 seg2 seg3 seg4
-- --------------- ----------- ----------- ----------- -----------
-- 75.158.2.101 75 158 2 101
-- 100.101.102.103 100 101 102 103
I need to do a similar sort in Oracle with something like:
select
ip_address,
'??1' as seg1,
'??2' as seg2,
'??3' as seg3,
'??4' as seg4
from
( select '75.158.2.101' ip_address from dual union all
select '100.101.102.103' from dual
)
order by
seg1,
seg2,
seg3,
seg4
Do I need to use (1) instr and (2) substr to break the string apart and reformat this or is there another more concise way of parsing and formatting this information?
Kent Waldrop