Last active
September 24, 2022 23:46
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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