简体   繁体   中英

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). How can we make these conversions in SQL?

I was trying to string together a method by converting IP address to binary and then ANDing them. There are a tonne of examples in general, nothing that works for PL/SQL. Any help is appreciated.

Here's a way using 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.

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


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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