Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Datatype for IP addresses Expand / Collapse
Author
Message
Posted Tuesday, October 14, 2008 5:48 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Friday, July 22, 2011 1:12 PM
Points: 27, Visits: 64
I'm looking for recommendations on what the best datatype would be that will store IP address information.

Sometimes we have a need to run reports for activity from IP addresses but want to exclude certain address ranges at times. Depending on what datatype you use the exclusion of address ranges can get tricky.
Post #585375
Posted Tuesday, October 14, 2008 6:16 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, August 10, 2010 5:07 AM
Points: 2,661, Visits: 23,078
The most flexible solution I have used is to store the IP address parts in individual fields and use a calculated field to concatenate it together. Then it becomes individual small integer fields.
Post #585390
Posted Tuesday, October 14, 2008 6:24 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Monday, March 17, 2014 6:21 AM
Points: 997, Visits: 6,477
maybe you should read this

http://sqlblog.com/blogs/denis_gobo/archive/2008/10/05/9266.aspx


-------------------------------------------------------------
"It takes 15 minutes to learn the game and a lifetime to master"
"Share your knowledge. It's a way to achieve immortality."


Post #585394
Posted Tuesday, December 1, 2009 10:36 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, March 10, 2012 8:06 PM
Points: 15, Visits: 51
I think a 4 byte binary field is the best. You can use substring function to pick out the octets and it takes the least amount of work to convert back and forth.

You can use a view or a computed column to convert the binary data back into a varchar(15) ip address as text if you need it.

You first use parsename ,then convert to tinyint, then convert to binary(1), then concatenate.

Here is a basic example with no error handling:
CREATE FUNCTION [dbo].[ipTxtToBin] 
(
@ip varchar(15)
)
RETURNS binary(4)
AS
BEGIN
DECLARE @o1 binary(1), @o2 binary(1), @o3 binary(1), @o4 binary(1)
SELECT
@o1 = CONVERT(binary(1),CONVERT(tinyint,PARSENAME(@ip,4))),
@o2 = CONVERT(binary(1),CONVERT(tinyint,PARSENAME(@ip,3))),
@o3 = CONVERT(binary(1),CONVERT(tinyint,PARSENAME(@ip,2))),
@o4 = CONVERT(binary(1),CONVERT(tinyint,PARSENAME(@ip,1)))
RETURN @o1+@o2+@o3+@o4
END

And to test it:
declare @ip VARCHAR(15) 
set @ip = '255.128.64.32'
declare @bin binary(4)
select @bin = [dbo].[ipTxtToBin] (@ip)
select @ip as ip
, @bin as bin
, convert(tinyint,substring(@bin,1,1)) as octet1
, convert(tinyint,substring(@bin,2,1)) as octet2
, convert(tinyint,substring(@bin,3,1)) as octet3
, convert(tinyint,substring(@bin,4,1)) as octet4

Result:
ip|bin|octet1|octet2|octet3|octet4
255.128.64.32|0xFF804020|255|128|64|32

You save between 3 to 11 bytes per IP address. You can filter on ranges using numeric assessments or through bitwise operations.

Here is a link to describe bit masks and CIDR if you want to use those ideas to filter your result:
http://www.countryipblocks.net/networking/identifying-the-network-and-broadcast-address-of-a-subnet/
Post #827200
Posted Thursday, December 3, 2009 8:57 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Today @ 6:53 AM
Points: 1,174, Visits: 2,655
Do you need to consider IPv6 addresses as well as IPv4?


edit: Just see the date on the original post!


--
Nigel
Useful Links:
How to post data/code on a forum to get the best help
The "Numbers" or "Tally" Table - Jeff Moden

Post #828270
Posted Saturday, December 5, 2009 4:56 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Tuesday, January 28, 2014 8:15 AM
Points: 3,065, Visits: 4,639
Melissa.Fischer (10/14/2008)
Depending on what datatype you use the exclusion of address ranges can get tricky.

why? anything is cast(able)


_____________________________________
Pablo (Paul) Berzukov

Author of Understanding Database Administration available at Amazon and other bookstores.

Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.
Post #829457
Posted Saturday, December 5, 2009 6:24 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 10:08 PM
Points: 35,362, Visits: 31,899
I split them to octets (using PARSENAME) stored as TINYINT and, like someone suggested, use a calculated column to reassemble them just to make life a little easier. The suggestion of binary storage is fine but the TINYINT method doesn't take any additional storage and humans seem to like it better. Subnet masking won't be a problem, either.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #829465
Posted Saturday, December 5, 2009 6:53 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Today @ 6:53 PM
Points: 31,177, Visits: 15,623
I'm with Jeff. I like them in separate fields, and then assemble them in another field if needed. As numerics they sort well, and the separation to me makes it easy to read when you're looking at the data.






Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #829470
Posted Monday, December 7, 2009 1:33 PM
SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, April 3, 2012 8:35 AM
Points: 1,608, Visits: 198
I used a char(15) data type with a rule for format.

Raymond Laubert
Exceptional DBA of 2009 Finalist
MCT, MCDBA, MCITP:SQL 2005 Admin,
MCSE, OCP:10g
Post #830241
Posted Monday, December 7, 2009 1:38 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, October 9, 2014 1:02 PM
Points: 6,032, Visits: 5,284
We had a BIG discussion on this recently.
http://www.sqlservercentral.com/articles/IPv4+Addresses/67534/

Be sure to review the discussion..
http://www.sqlservercentral.com/Forums/Topic768952-1617-1.aspx

CEWII
Post #830246
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse