postgresql - 在 postgresql 中以 ipv6 格式转换十六进制字符串
问题描述
我有一个像\xfc80000000000000ea508bfff217b628
bytea 格式的十六进制字符串,我想在选择查询中将其转换为fc80:0000:0000:0000:ea50:8bff:f217:b628
,我试过:
select '0:0:0:0:0:0:0:0'::inet + encode(ip::bytea,'hex') from a;
但以下错误即将到来
ERROR: operator does not exist: inet + text
LINE 1: select '0:0:0:0:0:0:0:0'::inet + encode(stationipv6::bytea,'...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
解决方案
substring()
使用bytea
值,您可以使用它来提取字节的各个部分以将其转换为inet
:
select concat_ws(':',
encode(substring(stationipv6, 1, 2), 'hex'),
encode(substring(stationipv6, 3, 2), 'hex'),
encode(substring(stationipv6, 5, 2), 'hex'),
encode(substring(stationipv6, 7, 2), 'hex'),
encode(substring(stationipv6, 9, 2), 'hex'),
encode(substring(stationipv6, 11, 2), 'hex'),
encode(substring(stationipv6, 13, 2), 'hex'),
encode(substring(stationipv6, 15, 2), 'hex')
)::inet
from your_table
在bytea
列上工作
推荐阅读
- kubernetes - 更新 pod 中的默认搜索域
- java - Map 接口是否扩展或实现了其他接口或类?
- java - 使用 jWindow 作为启动画面,显示效果很好,但图像不画……只是打开了空白的 jWindow……有什么想法吗?
- regex - 剂量 regexp_replace 不能在 postgresql 中使用多正则表达式(|)?否则我该怎么办?
- python - 您如何在一页中列出用户关注的所有帐户及其下方的相应项目?
- c++ - 编写了一个程序,显示与用户输入对应的斐波那契数,但它从零而不是一开始?
- javascript - 我该如何解决这个字符串长度问题?
- r - 编写 R 包时如何导入“%>%”?
- php - Laravel - SQLSTATE [2300]:完整性违规约束 - 数据库上的电子邮件不能为空错误
- python - BeautifulSoup fine_all NoneType 对象不可调用错误