MySQL上的inet_aton函数,Oracle和KingbaseES都没有,实现如下:
\set SQLTERM /
CREATE OR REPLACE FUNCTION inet_aton(ips in varchar2) RETURNS number AS
DECLARE rtn number;
DECLARE r varchar2;
DECLARE arg1 varchar2;
DECLARE arg2 varchar2;
DECLARE arg3 varchar2;
DECLARE arg4 varchar2;
BEGIN
r := '([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})';
arg1 := '\1';
arg2 := '\2';
arg3 := '\3';
arg4 := '\4';
execute immediate 'select to_number(regexp_replace(ip, '''||r||''', '''||arg1||''')) * 16777216 + to_number(regexp_replace(ip, '''||r||''', '''||arg2||''')) * 65536 + to_number(regexp_replace(ip, '''||r||''', '''||arg3||''')) * 256 + to_number(regexp_replace(ip, '''||r||''', '''||arg4||''')) as ip_number from (select '''||ips||''' as ip from dual)'
into rtn;
return rtn;
END;
/
\set SQLTERM ;
测试语句:select inet_aton('192.168.133.254') from dual;,结果如下:
[kingbase@ip ~]$ ksql -USYSTEM -W123456 iptest
ksql (V008R003C002B0100)
Type "help" for help.
iptest=# select inet_aton('192.168.133.254') from dual;
inet_aton
------------
3232269822
(1 row)