Oracle SQL - Get rows with values in IP address format (dotted quad)

梦想的初衷 提交于 2019-12-08 02:44:25

To validate the IP address, you could use REGEXP_LIKE.

  • Pattern : 0-255.0-255.0-255.0-255

For example,

SQL> WITH DATA AS(
  2  SELECT '10.20.30.40' ip_address FROM dual UNION ALL
  3  SELECT 'a.b.c.d' ip_address FROM dual UNION ALL
  4  SELECT '255.255.255.255' ip_address FROM dual
  5  )
  6  SELECT *
  7  FROM data
  8  WHERE REGEXP_LIKE(ip_address, '^(([0-9]{1}|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]{1}|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$');

IP_ADDRESS
---------------
10.20.30.40
255.255.255.255

SQL>

A value in these columns is either an IP address in dotted quad format

If you mean this :: by dotted quad, then you could still use the same REGEXP to validate the IP address.

  • Pattern : 0-255::0-255::0-255::0-255

For example,

SQL> WITH DATA AS(
  2  SELECT '10::20::30::40' ip_address FROM dual UNION ALL
  3  SELECT 'a.b.c.d' ip_address FROM dual UNION ALL
  4  SELECT '255.255.255.255' ip_address FROM dual
  5  )
  6  SELECT *
  7  FROM DATA
  8  WHERE REGEXP_LIKE(ip_address, '^(([0-9]{1}|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\::){3}([0-9]{1}|[0-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$');

IP_ADDRESS
---------------
10::20::30::40

SQL>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!