Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Datatype for IP addresses


Datatype for IP addresses

Author
Message
Melissa.Fischer
Melissa.Fischer
SSC Rookie
SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)SSC Rookie (27 reputation)

Group: General Forum Members
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.
Michael Earl-395764
Michael Earl-395764
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2707 Visits: 23078
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.
D.Oc
D.Oc
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1017 Visits: 6480
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."

davecason
davecason
Grasshopper
Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)Grasshopper (15 reputation)

Group: General Forum Members
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/
nigel.
nigel.
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1333 Visits: 2823
Do you need to consider IPv6 addresses as well as IPv4?


edit: Just see the date on the original post! Blush

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

PaulB-TheOneAndOnly
PaulB-TheOneAndOnly
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3093 Visits: 4639
Melissa.Fischer (10/14/2008)
Depending on what datatype you use the exclusion of address ranges can get tricky.

Blink 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.
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45131 Visits: 39923
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Steve Jones
Steve Jones
SSC-Dedicated
SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)SSC-Dedicated (36K reputation)

Group: Administrators
Points: 36150 Visits: 18751
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
My Blog: www.voiceofthedba.com
Ray Laubert
Ray Laubert
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1612 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
Elliott Whitlow
Elliott Whitlow
SSCertifiable
SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)SSCertifiable (6.2K reputation)

Group: General Forum Members
Points: 6208 Visits: 5314
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search