Unable to search between IPv6 range when converting string IP Address to VARBINARY(16)
2
votes
1
answer
1404
views
I am attempting to search for IPGeolocation data, which is presented by the data vendor as a start and end range for both IPv4 and IPv6. However, when I convert the IP that is provided, I am getting incorrect results.
To search by range, I am converting the
VARCHAR(39)
representation of the IPv6 into a VARBINARY
(i.e., Hex) and then using the BETWEEN
operator to see if the searched Hex value falls within a specific range.
**Query logic:**
SELECT CONVERT(VARBINARY(16), '2600:8800:6a06:2000:1d29:3b7:8c7c:271b')
**Search:**
IP: 2600:8800:6a06:2000:1d29:3b7:8c7c:271b
IpHex: 0x323630303A383830303A366130363A32
**Result 1**
IpStart: 2600:8800:490:0:0:0:0:0
IpStartHex: 0x323630303A383830303A3439303A303A
IpEnd: 2600:8800:77f:ffff:ffff:ffff:ffff:ffff
IpEndHex: 0x323630303A383830303A3737663A6666
**Result 2**
IpStart: 2600:8800:6a06:1d01:0:0:0:0
IpStartHex: 0x323630303A383830303A366130363A31
IpEnd: 2600:8800:6a06:24ff:ffff:ffff:ffff:ffff
IpEndHex: 0x323630303A383830303A366130363A32
Asked by J Weezy
(199 rep)
Feb 26, 2019, 05:46 PM
Last activity: Feb 26, 2019, 07:43 PM
Last activity: Feb 26, 2019, 07:43 PM