问题描述
最推荐在 SQL Server 中存储 IPv4 地址的数据类型是什么?
What should be the most recommended datatype for storing an IPv4 address in SQL server?
或者也许有人已经为它创建了一个用户 SQL 数据类型(.Net 程序集)?
我不需要排序.
推荐答案
将 IPv4 地址存储为 binary
(4) 最忠实于它所代表的内容,并允许轻松进行子网掩码样式的查询.但是,如果您实际上是在文本表示之后,则需要进行输入和输出转换.在这种情况下,您可能更喜欢字符串格式.
Storing an IPv4 address as a binary
(4) is truest to what it represents, and allows for easy subnet mask-style querying. However, it requires conversion in and out if you are actually after a text representation. In that case, you may prefer a string format.
一个很少使用的 SQL Server 函数,如果您将其存储为字符串可能会有所帮助 PARSENAME
,顺便说一句.不是为 IP 地址设计的,但非常适合它们.下面的调用将返回14":
A little-used SQL Server function that might help if you are storing as a string is PARSENAME
, by the way. Not designed for IP addresses but perfectly suited to them. The call below will return '14':
SELECT PARSENAME('123.234.23.14', 1)
(编号从右到左).
这篇关于在 SQL Server 中存储 IP 地址最合适的数据类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!