Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Expanding IP values based on subnet mask with increased complexity.

$
0
0

Hello Forum Members/Gurus'
Background:
I have a IP range as: 1.2.3.0/255.255.255.0

I want the following output:

1.2.3.0
 1.2.3.1
 1.2.3.2
 .
 .
 1.2.3.254
 1.2.3.255

Proposed script by Patrick Hurst works  like a charm.
CREATE TABLE test
  (
     ips VARCHAR(50)
  )

INSERT INTO test
VALUES     ('1.2.3.0/255.255.255.0'),
            ('12.13.14.15/255.255.255.0')

SELECT ips,
       Parsename(LEFT(ips, Charindex('/', ips)), 4)
       + '.'
       + Parsename(LEFT(ips, Charindex('/', ips)), 3)
       + '.'
       + Parsename(LEFT(ips, Charindex('/', ips)), 2)
       + '.' + Cast(number AS VARCHAR(3)) as ip
FROM   test
       CROSS apply (SELECT[number]
                    FROM   [master].[dbo].[spt_values]
                    WHERE  type = 'P'
                           AND number > 0
                           AND NUmber <= 255) d(number)

However if the third value changes from 255 to 254 the range should be from 1.2.2.1 to 1.2.3.255.

1.2.3.0/255.255.255.0        1.2.3.1 to 1.2.3.255
1.2.3.0/255.255.254.0        1.2.2.1 to 1.2.3.255
1.2.3.0/255.255.252.0        1.2.0.1 to 1.2.3.255

Kind Regards,

Sqlquery9


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>