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 はバイナリ データをうまく表示しません...