好吧 - 您可以将字符串硬'0xFF'
转换为NUMERIC(3)
, 以获得 255。
考虑到这一点 -
对于以冒号分隔的每个字符串标记
左填充字符串标记,'0'
长度为 4
连接由此获得的 8 个 4 字符字符串
在它们前面加上'0x'
和硬铸所有NUMERIC(38,0)
。
WITH
ipv6(ipv6) AS (
SELECT '2001:0569:7c0e:5700:1133:9bbd:232f:9c78'
UNION ALL SELECT '2001:569:7c0e:5700:1133:9bbd:232f:9c78'
)
SELECT
ipv6
, (
'0x'
|| LPAD(SPLIT_PART(ipv6,':',1 ),4,'0')
|| LPAD(SPLIT_PART(ipv6,':',2 ),4,'0')
|| LPAD(SPLIT_PART(ipv6,':',3 ),4,'0')
|| LPAD(SPLIT_PART(ipv6,':',4 ),4,'0')
|| LPAD(SPLIT_PART(ipv6,':',5 ),4,'0')
|| LPAD(SPLIT_PART(ipv6,':',6 ),4,'0')
|| LPAD(SPLIT_PART(ipv6,':',7 ),4,'0')
|| LPAD(SPLIT_PART(ipv6,':',8 ),4,'0')
)::NUMERIC(38,0) AS ipv6_2_int
, '0xff'::NUMERIC(3) AS hex_255
FROM ipv6;
-- out ipv6 | ipv6_2_int | hex_255
-- out -----------------------------------------+----------------------------------------+---------
-- out 2001:0569:7c0e:5700:1133:9bbd:232f:9c78 | 42540597931374402043102832032222256248 | 255
-- out 2001:569:7c0e:5700:1133:9bbd:232f:9c78 | 42540597931374402043102832032222256248 | 255