在SQL Server中存储IP地址最合适的数据类型是什么? [英] What is the most appropriate data type for storing an IP address in SQL server?
问题描述
在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函数可能是
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地址最合适的数据类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!