Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store both IPv4 or IPv6 in single column in SQL Server?

Should I use binary(16) or varbinary(16)?

I know I can use getAddress() in java.net.InetAddress (Java) or System.Net.IPAddress (C#) to get a byte[] representation of both IPv4 and IPv6, but if I need to insert IPv4 i.e. binary(4) into a binary(16) field in SQL Server, do I need to worry about padding or anything?

like image 535
Henry Avatar asked Aug 24 '10 23:08

Henry


People also ask

Can I have both IPv4 and IPv6 at the same time?

With the dual-stack solution, every networking device, server, switch, router, and firewall in an ISP's network will be configured with both IPv4 and IPv6 connectivity capabilities. Most importantly, dual-stack technology allows ISPs to process IPv4 and IPv6 data traffic simultaneously.

What is data type for IP address in SQL Server?

As with MS SQL Server you can store IP address, both IPv4 and IPv6, either as varchar or as numeric. As already mentioned, the binary representation is much more preferable than any other as it reflects the true nature of an IP address. SELECT (split_part('192.168. 1.1', '.

Which of the following is used to store IPv4 host address in SQL?

PostgreSQL offers data types to store IPv4, IPv6, and MAC addresses, as shown in Table 8.21. It is better to use these types instead of plain text types to store network addresses, because these types offer input error checking and specialized operators and functions (see Section 9.12).

Can you have multiple IPv6 addresses?

It's completely normal to have multiple IPv6 addresses on one device. A device generates new 64 bits every once in a while and uses that in the IPv6 address. Because new addresses are generated regularly the addresses are marked as temporary interfaces.


2 Answers

IF you store a binary(4) in a binary(16) column you'll get back, when you read it, a padded value of length 16. If you want to have dynamic length you must use a varbinary(16). This type retains the length of the data inserted, at the cost of adding extra 2 bytes on-disk (the actual length).

like image 109
Remus Rusanu Avatar answered Oct 04 '22 03:10

Remus Rusanu


Use v4-in-v6 address embedding to convert your ipv4 addresses to ipv6 format; then you can treat them all identically.

like image 25
zwol Avatar answered Oct 04 '22 03:10

zwol