Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

sql server - Split IPv4 address into 4 numbers in Oracle sql

I'm trying to split a given IPv4 address into four numbers.

In SQL Server this query works well for me:

select CAST (PARSENAME('10.20.30.40',4) AS INT) 

result: 10

select CAST (PARSENAME('10.20.30.40',3) AS INT)

result: 20

and so on.

I need the equivalent syntax in Oracle SQL, but can't find it. Any idea?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could use regexp_substr:

select ip,
       regexp_substr(ip, 'd+',1,1) as first_octet,
       regexp_substr(ip, 'd+',1,2) as second_octet,
       regexp_substr(ip, 'd+',1,3) as third_octet,
       regexp_substr(ip, 'd+',1,4) as fourth_octet
from  (select '10.20.30.40' AS ip from dual )ips;

Rextester Demo


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...