|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Monday, September 10, 2012 12:31 PM
Points: 56,
Visits: 129
|
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Thursday, May 09, 2013 4:22 PM
Points: 285,
Visits: 1,575
|
|
Nice article. I've used other commercial geo-location products in the past and it is nice to have a free solution that is able to get reasonable results for BI tasks without spending an arm and a leg. :)
One comment about the ConvertIP2Num function, it seems overly complicated to me. Perhaps something a little more simple might help readability and possibly enhance performance? Here is a relatively concise way to do the conversion:DECLARE @IPAddress VARCHAR(15) SET @IPAddress = '196.169.25.3'
--Convert to number SELECT CAST(PARSENAME(@IPAddress, 4) AS BIGINT) * POWER(256, 3) + CAST(PARSENAME(@IPAddress, 3) AS BIGINT) * POWER(256, 2) + CAST(PARSENAME(@IPAddress, 2) AS BIGINT) * POWER(256, 1) + CAST(PARSENAME(@IPAddress, 1) AS BIGINT) * POWER(256, 0)
|
|
|
|
|
SSCoach
         
Group: General Forum Members
Last Login: Yesterday @ 1:07 PM
Points: 18,733,
Visits: 12,332
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, March 21, 2011 10:44 AM
Points: 2,
Visits: 10
|
|
| IP's fit much better in binary(4) than bigint - the conversion is pretty simple and cuts down heavily on reads and index size
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, June 16, 2011 9:06 AM
Points: 3,
Visits: 49
|
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 5:54 PM
Points: 525,
Visits: 617
|
|
This was great. I am actually working with something like this. Got one question about the conversion function (ConvertIP2Num). Why do we need this section CAST((col%@SUBNET_MASK) as float)? The col contains our IP segment, such as 192, why do we need to get the mod? It's still going to be the same number in this case.
Thank you!
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Today @ 9:30 AM
Points: 1,500,
Visits: 18,174
|
|
Lamprey13 (7/10/2009)
Nice article. I've used other commercial geo-location products in the past and it is nice to have a free solution that is able to get reasonable results for BI tasks without spending an arm and a leg. :) One comment about the ConvertIP2Num function, it seems overly complicated to me. Perhaps something a little more simple might help readability and possibly enhance performance? Here is a relatively concise way to do the conversion: DECLARE @IPAddress VARCHAR(15) SET @IPAddress = '196.169.25.3'
--Convert to number SELECT CAST(PARSENAME(@IPAddress, 4) AS BIGINT) * POWER(256, 3) + CAST(PARSENAME(@IPAddress, 3) AS BIGINT) * POWER(256, 2) + CAST(PARSENAME(@IPAddress, 2) AS BIGINT) * POWER(256, 1) + CAST(PARSENAME(@IPAddress, 1) AS BIGINT) * POWER(256, 0)
I think both functions can be simplified
create function dbo.ConvertIp2Num(@ip nvarchar(15)) returns bigint as begin return ((cast(parsename(@ip,4) as bigint)*256+ cast(parsename(@ip,3) as bigint))*256+ cast(parsename(@ip,2) as bigint))*256+ cast(parsename(@ip,1) as bigint) end
create function dbo.ConvertNum2Ip(@ip bigint) returns nvarchar(15) as begin return cast(@ip/16777216 as nvarchar(15)) + '.' + cast((@ip/65536)%256 as nvarchar(15)) + '.' + cast((@ip/256)%256 as nvarchar(15)) + '.' + cast(@ip%256 as nvarchar(15)) end
____________________________________________________
How to get the best help on a forum
http://www.sqlservercentral.com/articles/Best+Practices/61537
Never approach a goat from the front, a horse from the rear, or a fool from any direction.
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Monday, September 10, 2012 12:31 PM
Points: 56,
Visits: 129
|
|
great feedback!
I like how simple and clean that looks
http://ozkary.blogspot.com http://og-bit.com
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Monday, August 23, 2010 7:41 AM
Points: 1,
Visits: 13
|
|
| How the heck does parsename grab the correct values? Looking at the description of PARSENAME it's meant to grab object/server names etc? Is it meant to parse strings like an IP address or it is just the way the object/server names are formatted allowing one to parse the IP address?
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Thursday, June 16, 2011 9:06 AM
Points: 3,
Visits: 49
|
|
It seems that currently parsename does not check for a valid identifier. As IP V4 address looks like 4 parts names. It works. Nice trick!
|
|
|
|