B
B
BigD2011-02-14 19:43:03
SQL
BigD, 2011-02-14 19:43:03

Checking if an IP address is in a range using SQL or PL/SQL?

Hello others.
Please tell me how to use SQL (preferably) or PL / SQL to implement a check for the occurrence of specific IP addresses in a particular range?
Simply put, you need to determine which subnet a particular IP address belongs to:
There is an ip_networks table of the format

RANGE_BEGIN RANGE_END RANGE_NAME<br/>
10.160.1.0 10.160.1.125 MSK<br/>
10.160.1.126 10.160.1.254 SPB

... and there are many more ...
and you need to display the value of the RANGE_NAME field in the select query for each IP address from a long list.
Something very long is spinning in my head using BETWEEN, FOR and CASE, but I suspect that this task should be solved somehow more elementarily ...
I really hope for you ...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bigbaraboom, 2011-02-14
@BigD

If the fields are of type inet, then:
SELECT * FROM ips WHERE inet '10.160.1.15' >= RANGE_BEGIN AND inet '10.160.1.15' <= RANGE_END ;

W
Wott, 2011-02-14
@Wott

Well, as much as possible!
Make an ip - unsigned int
record - the bottom ip from the
selection range - the first from the sorted list in ascending order, greater than or equal to the bottom ip from the range ,
while the index is used well and quickly
.
unoccupied blocks are also registered, but this is an offline operation and is done simply.
True, for a separate region there will be many of them. For the world wide, everything will be just fine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question