SQL query

Hi Guys,
I have to kind of repost this case because I'm stock with this.
I have a table with a column containing IP ranges, some fields contains more than one IP range.
See below:
IP = 172.27.88.0/24
IP = 172.27.28.0/24 | IP = 172.26.28.0/25 | IP = 127.27.28.129/25
IP = 172.27.14.0/23
IP = 172.27.169.0/25
In total I have 45 rows and in total there is 58 IP ranges.
I want to insert the IP ranges into a temp table seperately so I will get 58 rows in total.
I also have another value from another column that I need to bring along as well.
It's on SQL 2005.
Thanks in advance
Rasmus
ColoplastAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ColoplastAuthor Commented:
I forgot to mention that I want the ranges to be like 172.27.88.0/24 so the 'IP = ' and '|' stuff needs to filtered out.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
I remember a very similar question. You didn't get the solution you wanted?
0
ColoplastAuthor Commented:
Kind of...I just can't get 58 rows, my query only gives me 45 rows, so I don't get the fields that contains multiple IP ranges into my temp table.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Vitor MontalvãoMSSQL Senior EngineerCommented:
Can you post the query that you are using now?
0
ColoplastAuthor Commented:
Query:

CREATE TABLE #IP_Ranges(IPRange VARCHAR(18), IPLow VARCHAR(10), IPHigh VARCHAR(10))
DECLARE @id VARCHAR(200)
DECLARE myCursor CURSOR LOCAL FAST_FORWARD FOR
    SELECT [selection_criteria] AS 'ID' 
    FROM [dbo].[egroup]
	WHERE [selection_criteria] LIKE 'IP%'
OPEN myCursor
FETCH NEXT FROM myCursor INTO @id
WHILE @@FETCH_STATUS = 0 BEGIN
    
	DECLARE @IP_Row VARCHAR(200)
	DECLARE @pos SMALLINT
	SET @IP_Row = @id
	SET @IP_Row = REPLACE(@IP_Row,'IP = ', '')
	SET @pos = CHARINDEX('|',  @IP_Row)  
	WHILE @pos > 0
	BEGIN
	SET @IP_Row = SUBSTRING(@IP_Row, @pos+1, LEN(@IP_Row)-@pos)
	SET @pos  = CHARINDEX('|',  @IP_Row)  
	END
	
	DECLARE @CidrIP VARCHAR(200)
	SET @CidrIP = LTRIM(@IP_Row)
	
	INSERT INTO #IP_Ranges(IPRange, IPLow, IPHigh)
	VALUES(LTRIM(@IP_Row), dbo.[ConvertIPToLong](left(@CidrIP, patindex('%/%' , @CidrIP) - 1)) & (cast(4294967295 as bigint) ^ (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1)), dbo.[ConvertIPToLong](left(@CidrIP, patindex('%/%' , @CidrIP) - 1)) & (cast(4294967295 as bigint) ^ (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1)) + (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1))
FETCH NEXT FROM myCursor INTO @id
END
CLOSE myCursor
DEALLOCATE myCursor
SELECT * FROM #IP_Ranges
ORDER BY IPLow
DROP TABLE #IP_Ranges

Open in new window

0
Scott PletcherSenior DBACommented:
I have no idea what the computations on the IP address are doing.

The code below outputs the range as it displays in the original row.  I'm not sure what else "IP = 127.27.28.129/25" is supposed to translate to.

First create the split function below, as it is used by the main code.



SELECT 
    e.other_column_to_bring_along,
    IPLow,
    CASE WHEN CHARINDEX('/', ip_value) = 0 THEN IPLow
        ELSE PARSENAME(IPLow, 4) + '.' + PARSENAME(IPLow, 3) + '.' + PARSENAME(IPLow, 2) + '.' +
             + SUBSTRING(ip_value, CHARINDEX('/', ip_value + '/') + 1, 100) END AS IPHigh
    FROM (
        SELECT 
            1 AS other_column_to_bring_along,
            'IP = 172.27.28.0/24 | IP = 172.26.28.0/25 | IP = 127.27.28.129/25' AS [selection_criteria]
    ) AS e --[dbo].[egroup] e
    CROSS APPLY dbo.DelimitedSplit8K ( e.[selection_criteria], '|' ) AS ds
    CROSS APPLY ( SELECT LTRIM(RTRIM(REPLACE(REPLACE(ds.item, 'IP ', ''), '= ', ''))) AS ip_value ) AS assign_alias_1
    CROSS APPLY ( SELECT LEFT(ip_value, CHARINDEX('/', ip_value + '/') - 1) AS IPLow ) AS assign_alias_2
        WHERE e.[selection_criteria] LIKE 'IP%'

Open in new window

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER FUNCTION [dbo].[DelimitedSplit8K]
--===== Define I/O parameters
        (@pString VARCHAR(8000), @pDelimiter CHAR(1))
--WARNING!!! DO NOT USE MAX DATA-TYPES HERE!  IT WILL KILL PERFORMANCE!
RETURNS TABLE WITH SCHEMABINDING AS
 RETURN
--===== "Inline" CTE Driven "Tally Table" produces values from 1 up to 10,000...
     -- enough to cover VARCHAR(8000)
  WITH E1(N) AS (
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
                 SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
                ),                          --10E+1 or 10 rows
       E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
       E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
 cteTally(N) AS (--==== This provides the "base" CTE and limits the number of rows right up front
                     -- for both a performance gain and prevention of accidental "overruns"
                 SELECT TOP (ISNULL(DATALENGTH(@pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
                ),
cteStart(N1) AS (--==== This returns N+1 (starting position of each "element" just once for each delimiter)
                 SELECT 1 UNION ALL
                 SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(@pString,t.N,1) = @pDelimiter
                ),
cteLen(N1,L1) AS(--==== Return start and length (for use in substring)
                 SELECT s.N1,
                        ISNULL(NULLIF(CHARINDEX(@pDelimiter,@pString,s.N1),0)-s.N1,8000)
                   FROM cteStart s
                )
--===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found.
 SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
        Item       = LTRIM(RTRIM(SUBSTRING(@pString, l.N1, l.L1)))
   FROM cteLen l
;

Open in new window

0
ColoplastAuthor Commented:
Hi Vitor, is there any hope?
0
ColoplastAuthor Commented:
Oh thanks, I will try it out ;-)
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Sorry Coloplast, I didn't have time to check this. I thought you are using a solution I gave, so would be more easy working with my code.
I know I participated in that question but couldn't found it. You have the link for that question? May helps Scott's also.
0
ColoplastAuthor Commented:
The IP range is translated into an IPLow value and an IPHigh value which makes me able to evaluate if an IP address is part of the IP range.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Ok, found it.

And the solution I gave would return the numbers like this:
 172.27.28.0/24
 172.26.28.0/25
 127.27.28.129/25
DECLARE @IP_Row VARCHAR(100)
DECLARE @pos SMALLINT

SET @IP_Row='IP = 172.27.28.0/24 | IP = 172.26.28.0/25 | IP = 127.27.28.129/25'
SET @IP_Row=REPLACE(@IP_Row,'IP = ', '')

SET @pos  = CHARINDEX('|',  @IP_Row )  
WHILE @pos > 0
BEGIN

  PRINT LTRIM(SUBSTRING(@IP_Row, 1, @pos-1))

  SET @IP_Row = SUBSTRING(@IP_Row, @pos+1, LEN(@IP_Row)-@pos)
  SET @pos  = CHARINDEX('|',  @IP_Row )  
END

PRINT LTRIM(@IP_Row)

Open in new window

I can see that you used this solution. Just wondering what the [ConvertIPToLong] function does?
0
ColoplastAuthor Commented:
ConvertIPToLong does the following:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[ConvertIPToLong](@IP varchar(15))
RETURNS bigint
AS
BEGIN
    DECLARE @Long bigint
    SET @Long = CONVERT(bigint, PARSENAME(@IP, 4)) * 256 * 256 * 256 +
        CONVERT(bigint, PARSENAME(@IP, 3)) * 256 * 256 +
        CONVERT(bigint, PARSENAME(@IP, 2)) * 256 +
        CONVERT(bigint, PARSENAME(@IP, 1))

    RETURN (@Long)
END
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
So, what you mean is when it finds a row like 'IP = 172.27.28.0/24 | IP = 172.26.28.0/25 | IP = 127.27.28.129/25' it only inserts the last IP (127.27.28.129/25 for this case) and you want to have the 3 IP's inserted, right?
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
If so, then try this:
CREATE TABLE #IP_Ranges(IPRange VARCHAR(18), IPLow VARCHAR(10), IPHigh VARCHAR(10))
DECLARE @id VARCHAR(200)
DECLARE myCursor CURSOR LOCAL FAST_FORWARD FOR
    SELECT [selection_criteria] AS 'ID' 
    FROM [dbo].[egroup]
	WHERE [selection_criteria] LIKE 'IP%'
OPEN myCursor
FETCH NEXT FROM myCursor INTO @id
WHILE @@FETCH_STATUS = 0 BEGIN
    
	DECLARE @IP_Row VARCHAR(200)
	DECLARE @pos SMALLINT
	DECLARE @CidrIP VARCHAR(200)

	SET @IP_Row = @id
	SET @IP_Row = REPLACE(@IP_Row,'IP = ', '')
	SET @pos = CHARINDEX('|',  @IP_Row)  
	WHILE @pos > 0
	BEGIN
		SET @CidrIP = PRINT LTRIM(SUBSTRING(@IP_Row, 1, @pos-1))
		
		INSERT INTO #IP_Ranges(IPRange, IPLow, IPHigh)
		VALUES(LTRIM(@IP_Row), dbo.[ConvertIPToLong](left(@CidrIP, patindex('%/%' , @CidrIP) - 1)) & (cast(4294967295 as bigint) ^ (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1)), dbo.[ConvertIPToLong](left(@CidrIP, patindex('%/%' , @CidrIP) - 1)) & (cast(4294967295 as bigint) ^ (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1)) + (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1))
		
		SET @IP_Row = SUBSTRING(@IP_Row, @pos+1, LEN(@IP_Row)-@pos)
		SET @pos  = CHARINDEX('|',  @IP_Row)  
	END
	
	SET @CidrIP = PRINT LTRIM(SUBSTRING(@IP_Row, 1, @pos-1))
	
	INSERT INTO #IP_Ranges(IPRange, IPLow, IPHigh)
	VALUES(LTRIM(@IP_Row), dbo.[ConvertIPToLong](left(@CidrIP, patindex('%/%' , @CidrIP) - 1)) & (cast(4294967295 as bigint) ^ (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1)), dbo.[ConvertIPToLong](left(@CidrIP, patindex('%/%' , @CidrIP) - 1)) & (cast(4294967295 as bigint) ^ (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1)) + (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1))
FETCH NEXT FROM myCursor INTO @id
END
CLOSE myCursor
DEALLOCATE myCursor
SELECT * FROM #IP_Ranges
ORDER BY IPLow
DROP TABLE #IP_Ranges

Open in new window

0
ColoplastAuthor Commented:
Yes you are right.
When running you're query I get the following error:

Msg 156, Level 15, State 1, Line 64
Incorrect syntax near the keyword 'PRINT'.
Msg 156, Level 15, State 1, Line 73
Incorrect syntax near the keyword 'PRINT'.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Sorry, was from copy/paste. here you go:
CREATE TABLE #IP_Ranges(IPRange VARCHAR(18), IPLow VARCHAR(10), IPHigh VARCHAR(10))
DECLARE @id VARCHAR(200)
DECLARE myCursor CURSOR LOCAL FAST_FORWARD FOR
    SELECT [selection_criteria] AS 'ID' 
    FROM [dbo].[egroup]
	WHERE [selection_criteria] LIKE 'IP%'
OPEN myCursor
FETCH NEXT FROM myCursor INTO @id
WHILE @@FETCH_STATUS = 0 BEGIN
    
	DECLARE @IP_Row VARCHAR(200)
	DECLARE @pos SMALLINT
	DECLARE @CidrIP VARCHAR(200)

	SET @IP_Row = @id
	SET @IP_Row = REPLACE(@IP_Row,'IP = ', '')
	SET @pos = CHARINDEX('|',  @IP_Row)  
	WHILE @pos > 0
	BEGIN
		SET @CidrIP = LTRIM(SUBSTRING(@IP_Row, 1, @pos-1))
		
		INSERT INTO #IP_Ranges(IPRange, IPLow, IPHigh)
		VALUES(LTRIM(@IP_Row), dbo.[ConvertIPToLong](left(@CidrIP, patindex('%/%' , @CidrIP) - 1)) & (cast(4294967295 as bigint) ^ (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1)), dbo.[ConvertIPToLong](left(@CidrIP, patindex('%/%' , @CidrIP) - 1)) & (cast(4294967295 as bigint) ^ (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1)) + (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1))
		
		SET @IP_Row = SUBSTRING(@IP_Row, @pos+1, LEN(@IP_Row)-@pos)
		SET @pos  = CHARINDEX('|',  @IP_Row)  
	END
	
	SET @CidrIP = LTRIM(SUBSTRING(@IP_Row, 1, @pos-1))
	
	INSERT INTO #IP_Ranges(IPRange, IPLow, IPHigh)
	VALUES(LTRIM(@IP_Row), dbo.[ConvertIPToLong](left(@CidrIP, patindex('%/%' , @CidrIP) - 1)) & (cast(4294967295 as bigint) ^ (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1)), dbo.[ConvertIPToLong](left(@CidrIP, patindex('%/%' , @CidrIP) - 1)) & (cast(4294967295 as bigint) ^ (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1)) + (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1))
FETCH NEXT FROM myCursor INTO @id
END
CLOSE myCursor
DEALLOCATE myCursor
SELECT * FROM #IP_Ranges
ORDER BY IPLow
DROP TABLE #IP_Ranges

Open in new window

0
ColoplastAuthor Commented:
I get a lot of these errors:

(1 row(s) affected)
Msg 8152, Level 16, State 14, Line 66
String or binary data would be truncated.
The statement has been terminated.
Msg 8152, Level 16, State 14, Line 66
String or binary data would be truncated.
The statement has been terminated.
Msg 8152, Level 16, State 14, Line 66
String or binary data would be truncated.
The statement has been terminated.
Msg 536, Level 16, State 5, Line 73
Invalid length parameter passed to the SUBSTRING function.

(1 row(s) affected)
Msg 536, Level 16, State 5, Line 73
Invalid length parameter passed to the SUBSTRING function.

(1 row(s) affected)
Msg 536, Level 16, State 5, Line 73
Invalid length parameter passed to the SUBSTRING function.

(1 row(s) affected)
Msg 536, Level 16, State 5, Line 73
Invalid length parameter passed to the SUBSTRING function.

(1 row(s) affected)
Msg 536, Level 16, State 5, Line 73
Invalid length parameter passed to the SUBSTRING function.

(1 row(s) affected)
Msg 536, Level 16, State 5, Line 73
Invalid length parameter passed to the SUBSTRING function.

(1 row(s) affected)

(45 row(s) affected)
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
I think I got it. Please try again with this code:
CREATE TABLE #IP_Ranges(IPRange VARCHAR(18), IPLow VARCHAR(10), IPHigh VARCHAR(10))
DECLARE @id VARCHAR(200)
DECLARE myCursor CURSOR LOCAL FAST_FORWARD FOR
    SELECT [selection_criteria] AS 'ID' 
    FROM [dbo].[egroup]
	WHERE [selection_criteria] LIKE 'IP%'
OPEN myCursor
FETCH NEXT FROM myCursor INTO @id
WHILE @@FETCH_STATUS = 0 BEGIN
    
	DECLARE @IP_Row VARCHAR(200)
	DECLARE @pos SMALLINT
	DECLARE @CidrIP VARCHAR(200)

	SET @IP_Row = @id
	SET @IP_Row = REPLACE(@IP_Row,'IP = ', '')
	SET @pos = CHARINDEX('|',  @IP_Row)  
	WHILE @pos > 0
	BEGIN
		SET @CidrIP = LTRIM(SUBSTRING(@IP_Row, 1, @pos-1))
		
		INSERT INTO #IP_Ranges(IPRange, IPLow, IPHigh)
		VALUES(@CidrIP, dbo.[ConvertIPToLong](left(@CidrIP, patindex('%/%' , @CidrIP) - 1)) & (cast(4294967295 as bigint) ^ (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1)), 
			dbo.[ConvertIPToLong](left(@CidrIP, patindex('%/%' , @CidrIP) - 1)) & (cast(4294967295 as bigint) ^ (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1)) + (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1))
		
		SET @IP_Row = SUBSTRING(@IP_Row, @pos+1, LEN(@IP_Row)-@pos)
		SET @pos  = CHARINDEX('|',  @IP_Row)  
	END
	
	SET @CidrIP = LTRIM(SUBSTRING(@IP_Row, 1, @pos-1))
	
	INSERT INTO #IP_Ranges(IPRange, IPLow, IPHigh)
	VALUES(@CidrIP, dbo.[ConvertIPToLong](left(@CidrIP, patindex('%/%' , @CidrIP) - 1)) & (cast(4294967295 as bigint) ^ (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1)), 
		dbo.[ConvertIPToLong](left(@CidrIP, patindex('%/%' , @CidrIP) - 1)) & (cast(4294967295 as bigint) ^ (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1)) + (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1))
FETCH NEXT FROM myCursor INTO @id
END
CLOSE myCursor
DEALLOCATE myCursor
SELECT * FROM #IP_Ranges
ORDER BY IPLow
DROP TABLE #IP_Ranges

Open in new window

0
ColoplastAuthor Commented:
The query gives 58 rows :-), but I still the same errors as last time.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
It's bad to work without real data. Let's see if this now runs without errors:
CREATE TABLE #IP_Ranges(IPRange VARCHAR(18), IPLow VARCHAR(10), IPHigh VARCHAR(10))
DECLARE @id VARCHAR(200)
DECLARE myCursor CURSOR LOCAL FAST_FORWARD FOR
    SELECT [selection_criteria] AS 'ID' 
    FROM [dbo].[egroup]
	WHERE [selection_criteria] LIKE 'IP%'
OPEN myCursor
FETCH NEXT FROM myCursor INTO @id
WHILE @@FETCH_STATUS = 0 BEGIN
    
	DECLARE @IP_Row VARCHAR(200)
	DECLARE @pos SMALLINT
	DECLARE @CidrIP VARCHAR(200)

	SET @IP_Row = @id
	SET @IP_Row = REPLACE(@IP_Row,'IP = ', '')
	SET @pos = CHARINDEX('|',  @IP_Row)  
	WHILE @pos > 0
	BEGIN
		SET @CidrIP = LTRIM(SUBSTRING(@IP_Row, 1, @pos-1))
		
		INSERT INTO #IP_Ranges(IPRange, IPLow, IPHigh)
		VALUES(@CidrIP, dbo.[ConvertIPToLong](left(@CidrIP, patindex('%/%' , @CidrIP) - 1)) & (cast(4294967295 as bigint) ^ (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1)), 
			dbo.[ConvertIPToLong](left(@CidrIP, patindex('%/%' , @CidrIP) - 1)) & (cast(4294967295 as bigint) ^ (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1)) + (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1))
		
		SET @IP_Row = SUBSTRING(@IP_Row, @pos+1, LEN(@IP_Row)-@pos)
		SET @pos  = CHARINDEX('|',  @IP_Row)  
	END
	
	SET @CidrIP = @IP_Row
	
	INSERT INTO #IP_Ranges(IPRange, IPLow, IPHigh)
	VALUES(@CidrIP, dbo.[ConvertIPToLong](left(@CidrIP, patindex('%/%' , @CidrIP) - 1)) & (cast(4294967295 as bigint) ^ (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1)), 
		dbo.[ConvertIPToLong](left(@CidrIP, patindex('%/%' , @CidrIP) - 1)) & (cast(4294967295 as bigint) ^ (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1)) + (Power(2, 32 - Cast(substring(@CidrIP, patindex('%/%' , @CidrIP) + 1, 2) as int)) - 1))
FETCH NEXT FROM myCursor INTO @id
END
CLOSE myCursor
DEALLOCATE myCursor
SELECT * FROM #IP_Ranges
ORDER BY IPLow
DROP TABLE #IP_Ranges

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ColoplastAuthor Commented:
It seems to be working now ;-)
I'm getting the data.
Thanks for your effort Vitor.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Great :)
You are welcome.

Cheers.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.