Skip to content
Advertisement

Select record between two IP ranges

I have a table which stores a ID, Name, Code, IPLow, IPHigh such as:

Now, if I have an IP address 192.168.2.50, how can I retrieve the matching record?

Edit

Based on Gordon’s answer (which I’m getting compilation errors) this is what I have:

but this gives me an error:

Any ideas?

Advertisement

Answer

Painfully. SQL Server has lousy string manipulation functions. It does, however, offer parsename(). This approach converts the IP address to a large decimal value for the comparison:

I should note that IP addresses are often stored in the database as the 4-byte unsigned integers. This makes comparisons much easier . . . although you need complicated logic (usually wrapped in a function) to convert the values to a readable format.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement