Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Determine the IP range an IP address falls in

Author  Topic 

MuffinMan
Posting Yak Master

107 Posts

Posted - 2001-05-31 : 12:58:39
All,

I have a need to determine the IP range an IP address falls in.

Example:

set nocount on

create table #hub ( HubName varchar(20) , Start_Ip varchar(15) , End_Ip varchar(15) )

Insert #hub Select 'Hub1' , '10.10.0.1' , '10.10.0.255'
Insert #hub Select 'Hub2' , '100.10.0.1' , '100.10.0.255'
Insert #hub Select 'Hub3' , '1.10.0.1' , '1.10.0.255'
Insert #hub Select 'Hub4' , '001.010.000.001' , '001.010.000.255'

declare @ip varchar(15)

-- falls in Hub3's range
select @ip = '1.10.0.2'
select * from #hub where @ip >= start_ip and @ip <= end_ip

-- PROBLEM: falls within Hub3's range, but won't be found by SQL
select @ip = '1.10.0.6'
select * from #hub where @ip >= start_ip and @ip <= end_ip

-- falls in Hub3's range
select @ip = '1.10.0.10'
select * from #hub where @ip >= start_ip and @ip <= end_ip

-- padded: falls within Hub4's range
select @ip = '001.010.000.006'
select * from #hub where @ip >= start_ip and @ip <= end_ip

drop table #hub


As you can see, the IP '1.10.0.6' clearly falls in the range '1.10.0.1' to '1.10.0.255'. However, since we are comparing varchar values, the SQL query does NOT return a match.

In our production database, the HUB table contains 7,000 records (relatively small at this point, but could grow tremendously in the future).

Padding the IP's would take care of the issue ('001.010.000.006' falls between '001.010.000.001' to '001.010.000.255') but how would you do this on a record by record basis (and would you want to)?

This lookup will be done by help desk rep's, so the response must be quick and efficient, since multiple rep's (50-250) could be running this continuously throughout the day.

One last detail: we CANNOT store the start and end IP's in the HUB table with padding -- the database is proprietary and cannot be altered.

   

- Advertisement -