<tfoot id='rkzgS'></tfoot>

    <i id='rkzgS'><tr id='rkzgS'><dt id='rkzgS'><q id='rkzgS'><span id='rkzgS'><b id='rkzgS'><form id='rkzgS'><ins id='rkzgS'></ins><ul id='rkzgS'></ul><sub id='rkzgS'></sub></form><legend id='rkzgS'></legend><bdo id='rkzgS'><pre id='rkzgS'><center id='rkzgS'></center></pre></bdo></b><th id='rkzgS'></th></span></q></dt></tr></i><div id='rkzgS'><tfoot id='rkzgS'></tfoot><dl id='rkzgS'><fieldset id='rkzgS'></fieldset></dl></div>
      <bdo id='rkzgS'></bdo><ul id='rkzgS'></ul>
      <legend id='rkzgS'><style id='rkzgS'><dir id='rkzgS'><q id='rkzgS'></q></dir></style></legend>

        <small id='rkzgS'></small><noframes id='rkzgS'>

      1. 用于在 SQL Server 中存储 ip 地址的数据类型

        时间:2023-07-17
        <tfoot id='E9CZW'></tfoot>

        <i id='E9CZW'><tr id='E9CZW'><dt id='E9CZW'><q id='E9CZW'><span id='E9CZW'><b id='E9CZW'><form id='E9CZW'><ins id='E9CZW'></ins><ul id='E9CZW'></ul><sub id='E9CZW'></sub></form><legend id='E9CZW'></legend><bdo id='E9CZW'><pre id='E9CZW'><center id='E9CZW'></center></pre></bdo></b><th id='E9CZW'></th></span></q></dt></tr></i><div id='E9CZW'><tfoot id='E9CZW'></tfoot><dl id='E9CZW'><fieldset id='E9CZW'></fieldset></dl></div>

        <small id='E9CZW'></small><noframes id='E9CZW'>

          <tbody id='E9CZW'></tbody>
          <bdo id='E9CZW'></bdo><ul id='E9CZW'></ul>

                • <legend id='E9CZW'><style id='E9CZW'><dir id='E9CZW'><q id='E9CZW'></q></dir></style></legend>

                  本文介绍了用于在 SQL Server 中存储 ip 地址的数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我应该选择什么数据类型来在 SQL Server 中存储 IP 地址?

                  What datatype should I choose for storing an IP Address in a SQL Server?

                  通过选择正确的数据类型,按 IP 地址过滤是否足够容易?

                  By selecting the right datatype would it be easy enough to filter by IP address then?

                  推荐答案

                  技术上正确的 IPv4 存储方法是二进制 (4),因为它实际上是这样的(不,甚至不是 INT32/INT(4),我们都知道和喜爱的数字文本形式(255.255.255.255)只是其二进制内容的显示转换).

                  The technically correct way to store IPv4 is binary(4), since that is what it actually is (no, not even an INT32/INT(4), the numeric textual form that we all know and love (255.255.255.255) being just the display conversion of its binary content).

                  如果你这样做,你会希望函数与文本显示格式相互转换:

                  If you do it this way, you will want functions to convert to and from the textual-display format:

                  以下是将文本显示形式转换为二进制的方法:

                  Here's how to convert the textual display form to binary:

                  CREATE FUNCTION dbo.fnBinaryIPv4(@ip AS VARCHAR(15)) RETURNS BINARY(4)
                  AS
                  BEGIN
                      DECLARE @bin AS BINARY(4)
                  
                      SELECT @bin = CAST( CAST( PARSENAME( @ip, 4 ) AS INTEGER) AS BINARY(1))
                                  + CAST( CAST( PARSENAME( @ip, 3 ) AS INTEGER) AS BINARY(1))
                                  + CAST( CAST( PARSENAME( @ip, 2 ) AS INTEGER) AS BINARY(1))
                                  + CAST( CAST( PARSENAME( @ip, 1 ) AS INTEGER) AS BINARY(1))
                  
                      RETURN @bin
                  END
                  go
                  

                  这里是如何将二进制转换回文本显示形式:

                  And here's how to convert the binary back to the textual display form:

                  CREATE FUNCTION dbo.fnDisplayIPv4(@ip AS BINARY(4)) RETURNS VARCHAR(15)
                  AS
                  BEGIN
                      DECLARE @str AS VARCHAR(15) 
                  
                      SELECT @str = CAST( CAST( SUBSTRING( @ip, 1, 1) AS INTEGER) AS VARCHAR(3) ) + '.'
                                  + CAST( CAST( SUBSTRING( @ip, 2, 1) AS INTEGER) AS VARCHAR(3) ) + '.'
                                  + CAST( CAST( SUBSTRING( @ip, 3, 1) AS INTEGER) AS VARCHAR(3) ) + '.'
                                  + CAST( CAST( SUBSTRING( @ip, 4, 1) AS INTEGER) AS VARCHAR(3) );
                  
                      RETURN @str
                  END;
                  go
                  

                  这是如何使用它们的演示:

                  Here's a demo of how to use them:

                  SELECT dbo.fnBinaryIPv4('192.65.68.201')
                  --should return 0xC04144C9
                  go
                  
                  SELECT dbo.fnDisplayIPv4( 0xC04144C9 )
                  -- should return '192.65.68.201'
                  go
                  

                  最后,在进行查找和比较时,如果您希望能够利用索引,请始终使用二进制形式.

                  Finally, when doing lookups and compares, always use the binary form if you want to be able to leverage your indexes.

                  更新:

                  我想添加一种方法来解决 SQL Server 中标量 UDF 的固有性能问题,但仍然保留函数的代码重用是使用 iTVF(内联表值函数).下面是如何将上面的第一个函数(字符串到二进制)重写为 iTVF:

                  I wanted to add that one way to address the inherent performance problems of scalar UDFs in SQL Server, but still retain the code-reuse of a function is to use an iTVF (inline table-valued function) instead. Here's how the first function above (string to binary) can be re-written as an iTVF:

                  CREATE FUNCTION dbo.itvfBinaryIPv4(@ip AS VARCHAR(15)) RETURNS TABLE
                  AS RETURN (
                      SELECT CAST(
                                 CAST( CAST( PARSENAME( @ip, 4 ) AS INTEGER) AS BINARY(1))
                              +  CAST( CAST( PARSENAME( @ip, 3 ) AS INTEGER) AS BINARY(1))
                              +  CAST( CAST( PARSENAME( @ip, 2 ) AS INTEGER) AS BINARY(1))
                              +  CAST( CAST( PARSENAME( @ip, 1 ) AS INTEGER) AS BINARY(1))
                                  AS BINARY(4)) As bin
                          )
                  go
                  

                  示例如下:

                  SELECT bin FROM dbo.fnBinaryIPv4('192.65.68.201')
                  --should return 0xC04144C9
                  go
                  

                  这是在 INSERT 中使用它的方法

                  And here's how you would use it in an INSERT

                  INSERT INTo myIpTable
                  SELECT {other_column_values,...},
                         (SELECT bin FROM dbo.itvfBinaryIPv4('192.65.68.201'))
                  

                  这篇关于用于在 SQL Server 中存储 ip 地址的数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在一列上选择 DISTINCT 下一篇:GROUP BY 组合/连接一列

                  相关文章

                    • <bdo id='YJuy2'></bdo><ul id='YJuy2'></ul>
                      <legend id='YJuy2'><style id='YJuy2'><dir id='YJuy2'><q id='YJuy2'></q></dir></style></legend>

                    1. <tfoot id='YJuy2'></tfoot>
                    2. <i id='YJuy2'><tr id='YJuy2'><dt id='YJuy2'><q id='YJuy2'><span id='YJuy2'><b id='YJuy2'><form id='YJuy2'><ins id='YJuy2'></ins><ul id='YJuy2'></ul><sub id='YJuy2'></sub></form><legend id='YJuy2'></legend><bdo id='YJuy2'><pre id='YJuy2'><center id='YJuy2'></center></pre></bdo></b><th id='YJuy2'></th></span></q></dt></tr></i><div id='YJuy2'><tfoot id='YJuy2'></tfoot><dl id='YJuy2'><fieldset id='YJuy2'></fieldset></dl></div>

                      <small id='YJuy2'></small><noframes id='YJuy2'>