question

Anthony Osborn avatar image
Anthony Osborn asked

How should I store an IP address in SQL Server?

How should I store an IP address in SQL Server?

ip-address
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Matt Whitfield avatar image
Matt Whitfield answered

4 tinyint's gets my vote for an IPv4 address, and I would probably store an IPv6 address in a binary(16).

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

dave ballantyne avatar image
dave ballantyne answered

My personal preference would be 4 tinyint's.

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Blackhawk-17 avatar image
Blackhawk-17 answered

It depends...

  • How much manipulation are you doing on them?
  • What kind of querying will be done?
  • What indexing will be required?

Four tiny INTs, as has been suggested, is a good solution if you will be looking at particular octets and/or doing manipulating/grouping at that level.

A VARCHAR(15) if you are going for plain, basic storage of an IPv4 - including the periods. Best suited for simple inventory.

2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

I think the Ask team should code in that you get automatic rep for starting an answer with 'it depends' :)
1 Like 1 ·
It should also be a macro in the Edit dialogue ;)
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.