ORACLE分解IP地址

来源:互联网 发布:iphone怎么传文件到mac 编辑:程序博客网 时间:2024/04/26 00:37

通过使用内置函数substr和instr来分解IP地址。看个下面的例子。

SQL> col a format a10
SQL> col b format a10
SQL> col c format a10
SQL> col d format a10
SQL> select ip,
  2  substr(ip,1,instr(ip,'.',1,1)-1) a,
  3  substr(ip,instr(ip,'.',1,1)+1,instr(ip,'.',1,2)-instr(ip,'.',1,1)-1) b,
  4  substr(ip,instr(ip,'.',1,2)+1,instr(ip,'.',1,3)-instr(ip,'.',1,2)-1) c,
  5  substr(ip,instr(ip,'.',1,3)+1) d
  6  from
  7  (select '192.168.1.1' as ip from t1);

IP          A          B          C          D
----------- ---------- ---------- ---------- ----------
192.168.1.1 192        168        1          1

 

再换一个IP来试试

 

SQL> select ip,
  2  substr(ip,1,instr(ip,'.',1,1)-1) a,
  3  substr(ip,instr(ip,'.',1,1)+1,instr(ip,'.',1,2)-instr(ip,'.',1,1)-1) b,
  4  substr(ip,instr(ip,'.',1,2)+1,instr(ip,'.',1,3)-instr(ip,'.',1,2)-1) c,
  5  substr(ip,instr(ip,'.',1,3)+1) d
  6  from
  7  (select '192.168.99.198' as ip from t1);

IP             A          B          C          D
-------------- ---------- ---------- ---------- ----------
192.168.99.198 192        168        99         198

 

呵呵 还挺通用的哦