简体   繁体   English

在 Oracle 12c/PL SQL 中查找给定 IP 地址和前缀长度的子网地址

[英]Find Subnet Address given IP Address and Prefix Length in Oracle 12c/ PL SQL

Given IP Address (example: 12.12.12.12/24) return prefix IP address (example: 12.12.12.0).给定 IP 地址(例如:12.12.12.12/24)返回前缀 IP 地址(例如:12.12.12.0)。 How can we make these conversions in SQL?我们如何在 SQL 中进行这些转换?

I was trying to string together a method by converting IP address to binary and then ANDing them.我试图通过将 IP 地址转换为二进制然后对它们进行 AND 运算来将一种方法串在一起。 There are a tonne of examples in general, nothing that works for PL/SQL.总的来说,有大量示例,但没有任何适用于 PL/SQL 的示例。 Any help is appreciated.任何帮助表示赞赏。

Here's a way using REGEXP_REPLACE().这是使用 REGEXP_REPLACE() 的一种方法。 The regular expression describes the string, with the 'remembered' part in parentheses.正则表达式描述字符串,括号中是“记住”部分。 The remembered part is everything up to and including the last period in the string.记住的部分是直到并包括字符串中最后一个句点的所有内容。 We don;t want the rest of the string.我们不想要字符串的其余部分。 The replace part of the call replaces with the first remembered part (denoted as \\1) followed by the 0.调用的替换部分替换为第一个记住的部分(表示为 \\1),后跟 0。

SELECT REGEXP_REPLACE('12.12.12.12/24', '(.*\.).*', '\10') new_ip
from dual;


NEW_IP    
----------
12.12.12.0
1 row selected.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM