Skip to content

Instantly share code, notes, and snippets.

@Xophmeister
Last active September 24, 2022 23:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xophmeister/5141494 to your computer and use it in GitHub Desktop.
Save Xophmeister/5141494 to your computer and use it in GitHub Desktop.
Oracle SQL and PL/SQL function to convert proper IPv4 address (i.e., 32-bit integer) into the standard, "dotted" format
-- We have a table (ip_log) of proper IPv4 addresses (ip)
select bitand(ip / 16777216, 255) || '.' || bitand(ip / 65536, 255) || '.' || bitand(ip / 256, 255) || '.' || bitand(ip, 255) ip
from ip_log;
-- ...or a function to do the same:
create or replace function long2ip(ip in number)
return varchar2 deterministic
as
begin
return bitand(ip / 16777216, 255) || '.' ||
bitand(ip / 65536, 255) || '.' ||
bitand(ip / 256, 255) || '.' ||
bitand(ip, 255);
end long2ip;
/
-- Now we can do:
select long2ip(ip) ip from ip_log;
select long2ip(167772161) ip from dual; -- 10.0.0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment