SQL Clone
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
Valued Member
Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)

Group: General Forum Members
Points: 61 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
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13305 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 (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1239 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
SSC Journeyman
SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)SSC Journeyman (97 reputation)

Group: General Forum Members
Points: 97 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.
Hall of Fame
Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)Hall of Fame (3.3K reputation)

Group: General Forum Members
Points: 3263 Visits: 2907
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
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14225 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 Guru
SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)SSC Guru (216K reputation)

Group: General Forum Members
Points: 216132 Visits: 41986
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Steve Jones
Steve Jones
SSC Guru
SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)SSC Guru (147K reputation)

Group: Administrators
Points: 147121 Visits: 19434
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.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1762 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
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24154 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