I'd suggest to add two additional columns, i.e. ipStartNum
and ipEndNum
, where you convert the ip string value to a number according, for example, using the following formula (this formula is taken from this SO answer, please upvote if it helps you). Then, when searching for the office based on a particular ip, transform this ip into such a number and do a BETWEEN-query:
CREATE TABLE actionlog
("id" int, "ipStart" varchar2(12), "ipEnd" varchar2(12), "officeName" varchar2(12), "ipStartNum" number, "ipEndNum" number)
\\
INSERT ALL
INTO actionlog ("id", "ipStart", "ipEnd", "officeName")
VALUES (0, '127.0.0.1', '127.0.0.1', 'local office')
INTO actionlog ("id", "ipStart", "ipEnd", "officeName")
VALUES (1, '129.127.0.0', '129.127.0.12', 'test office')
INTO actionlog ("id", "ipStart", "ipEnd", "officeName")
VALUES (2, '132.127.0.11', '132.127.0.22', 'test office2')
SELECT * FROM dual
\\
update actionlog set "ipStartNum" = to_number(regexp_substr("ipStart", '\d+', 1, 1)) * 16777216 +
to_number(regexp_substr("ipStart", '\d+', 1, 2)) * 65536 +
to_number(regexp_substr("ipStart", '\d+', 1, 3)) * 256 +
to_number(regexp_substr("ipStart", '\d+', 1, 4))
\\
update actionlog set "ipEndNum" = to_number(regexp_substr("ipEnd", '\d+', 1, 1)) * 16777216 +
to_number(regexp_substr("ipEnd", '\d+', 1, 2)) * 65536 +
to_number(regexp_substr("ipEnd", '\d+', 1, 3)) * 256 +
to_number(regexp_substr("ipEnd", '\d+', 1, 4))
\\
select * from actionlog
where to_number(regexp_substr('129.127.0.5', '\d+', 1, 1)) * 16777216 +
to_number(regexp_substr('129.127.0.5', '\d+', 1, 2)) * 65536 +
to_number(regexp_substr('129.127.0.5', '\d+', 1, 3)) * 256 +
to_number(regexp_substr('129.127.0.5', '\d+', 1, 4)) between "ipStartNum" and "ipEndNum"
\\
Note: to keep values of ipStartNum
and ipEndNum
up to date, you could probably use calculated columns, triggers, or java-application based logic as well. See solution based on calculated columns below:
CREATE TABLE actionlog
("id" int, "ipStart" varchar2(12), "ipEnd" varchar2(12), "officeName" varchar2(12),
"ipStartNum" as (to_number(regexp_substr("ipStart", '\d+', 1, 1)) * 16777216 +
to_number(regexp_substr("ipStart", '\d+', 1, 2)) * 65536 +
to_number(regexp_substr("ipStart", '\d+', 1, 3)) * 256 +
to_number(regexp_substr("ipStart", '\d+', 1, 4))),
"ipEndNum" as (to_number(regexp_substr("ipEnd", '\d+', 1, 1)) * 16777216 +
to_number(regexp_substr("ipEnd", '\d+', 1, 2)) * 65536 +
to_number(regexp_substr("ipEnd", '\d+', 1, 3)) * 256 +
to_number(regexp_substr("ipEnd", '\d+', 1, 4))))