1

我正在使用 SQL Server 2005。

我试图弄清楚如何读取存储为字符串的 IPv6 地址,然后将其扩展为完整的 8 个八位字节表示法,并将每个八位字节转换为二进制,然后将所有二进制表示加在一起最终将地址转换为二进制.

这是完整的符号:2001:0db8:85a3:0000:0000:8a2e:0370:7334 与我们数据库中存储的相同的短符号:2001:0db8:85a3::8a2e:0370:7334

下面是另一个有效 IPv6 地址的示例::::1

维基百科的规则很好地列出并在这里解释:http ://en.wikipedia.org/wiki/IPv6#Address_format

我不确定如何将 IPv6 地址扩展为完整符号,并希望这里有人已经弄清楚或知道如何做到这一点。我用谷歌搜索了几个小时,但找不到 SQL Server 2005 的任何解决方案。

一旦地址扩展,我正在考虑根据 : 拆分每个八位字节,并使用此处概述的方法转换为二进制:http: //blogs.msdn.com/b/sqltips/archive/2008/07/02/将十六进制字符串转换为 varbinary-and-vice-versa.aspx

非常感谢任何帮助!

4

1 回答 1

1

SQL小提琴

样品表

select '2001:0db8:85a3:0000:0000:8a2e:0370:7334' address
  into ipv6
  union all
select '2001:db8:85a3::8a2e:0370:7334' union all
select '2001:0db8:85a3::' union all
select '::ff01:0db8:85a3' union all
select '::1' union all
select '002::' union all
select '::' union all
select '1:2:3:4:5:6:7:' union all
select ':A:2:3:4:5:6:7' union all
select 'F:2:3:4:5:6::8' union all
select '' union all -- invalid
select null; -- null test

查询

with step1(address,full8) as(
     select address,
            replace(nullif(address,''),'::',replicate(':',9-len(address)+len(replace(address,':',''))))
       from ipv6
), step2(address,full8,xml) as (
     select address, full8,
            cast('<x>'+replace(full8,':','</x><x>')+'</x>' as xml)
       from step1
), step3(address,full8,xml,part,pos) as (
     select address, full8,xml,
            right('0000'+part.value('.','varchar(4)'),4),
            part.value('for $s in . return count(../*[. << $s]) + 1', 'int') pos
       from step2
cross apply xml.nodes('x') node(part)
), step4(address,hex) as (
     select o.address, (select i.part+''
                          from step3 i
                         where i.address=o.address
                      order by i.pos
                           for xml path('')) hex
       from step3 o
group by address
)
     select address,
            hex,
            cast('' as xml).value(
              'xs:hexBinary(sql:column("hex"))','binary(16)') bin
       from step4
   order by address;

结果

|                                 ADDRESS |                              HEX |                                               BIN |
----------------------------------------------------------------------------------------------------------------------------------
|                                      :: | 00000000000000000000000000000000 |                   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
|                                     ::1 | 00000000000000000000000000000001 |                   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 |
|                        ::ff01:0db8:85a3 | 00000000000000000000ff010db885a3 |          0,0,0,0,0,0,0,0,0,0,-1,1,13,-72,-123,-93 |
|                          :A:2:3:4:5:6:7 | 0000000A000200030004000500060007 |                  0,0,0,10,0,2,0,3,0,4,0,5,0,6,0,7 |
|                                   002:: | 00020000000000000000000000000000 |                   0,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0 |
|                          1:2:3:4:5:6:7: | 00010002000300040005000600070000 |                   0,1,0,2,0,3,0,4,0,5,0,6,0,7,0,0 |
|                        2001:0db8:85a3:: | 20010db885a300000000000000000000 |          32,1,13,-72,-123,-93,0,0,0,0,0,0,0,0,0,0 |
| 2001:0db8:85a3:0000:0000:8a2e:0370:7334 | 20010db885a3000000008a2e03707334 | 32,1,13,-72,-123,-93,0,0,0,0,-118,46,3,112,115,52 |
|           2001:db8:85a3::8a2e:0370:7334 | 20010db885a3000000008a2e03707334 | 32,1,13,-72,-123,-93,0,0,0,0,-118,46,3,112,115,52 |
|                          F:2:3:4:5:6::8 | 000F0002000300040005000600000008 |                  0,15,0,2,0,3,0,4,0,5,0,6,0,0,0,8 |

注意:SQL Fiddle 不能很好地显示二进制数据...

于 2012-12-13T05:41:06.420 回答