You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在SQL中重新整理Blob类型person_profile_id的HEX字符串格式?

可行,以下是不同数据库的实现方案

MySQL/MariaDB

可以通过字符串截取拼接或插入分隔符的方式实现:

-- 截取拼接写法
SELECT CONCAT(
  SUBSTRING(HEX(person_profile_id), 1, 8), '-',
  SUBSTRING(HEX(person_profile_id), 9, 4), '-',
  SUBSTRING(HEX(person_profile_id), 13, 4), '-',
  SUBSTRING(HEX(person_profile_id), 17, 4), '-',
  SUBSTRING(HEX(person_profile_id), 21)
) AS formatted_person_profile_id
FROM person_profile;

-- 插入分隔符写法
SELECT INSERT(INSERT(INSERT(INSERT(
  HEX(person_profile_id),
  21, 0, '-'),
  17, 0, '-'),
  13, 0, '-'),
  9, 0, '-'
) AS formatted_person_profile_id
FROM person_profile;

PostgreSQL

支持正则替换或截取拼接两种方式:

-- 截取拼接写法
SELECT CONCAT(
  SUBSTRING(ENCODE(person_profile_id, 'hex'), 1, 8), '-',
  SUBSTRING(ENCODE(person_profile_id, 'hex'), 9, 4), '-',
  SUBSTRING(ENCODE(person_profile_id, 'hex'), 13, 4), '-',
  SUBSTRING(ENCODE(person_profile_id, 'hex'), 17, 4), '-',
  SUBSTRING(ENCODE(person_profile_id, 'hex'), 21)
) AS formatted_person_profile_id
FROM person_profile;

-- 正则替换写法
SELECT REGEXP_REPLACE(ENCODE(person_profile_id, 'hex'), '([0-9A-F]{8})([0-9A-F]{4})([0-9A-F]{4})([0-9A-F]{4})([0-9A-F]{12})', '\1-\2-\3-\4-\5') AS formatted_person_profile_id
FROM person_profile;

SQL Server

通过类型转换后截取拼接实现:

SELECT CONCAT(
  SUBSTRING(CONVERT(VARCHAR(MAX), person_profile_id, 2), 1, 8), '-',
  SUBSTRING(CONVERT(VARCHAR(MAX), person_profile_id, 2), 9, 4), '-',
  SUBSTRING(CONVERT(VARCHAR(MAX), person_profile_id, 2), 13, 4), '-',
  SUBSTRING(CONVERT(VARCHAR(MAX), person_profile_id, 2), 17, 4), '-',
  SUBSTRING(CONVERT(VARCHAR(MAX), person_profile_id, 2), 21)
) AS formatted_person_profile_id
FROM person_profile;

内容的提问来源于stack exchange,提问作者margercastigador

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 19:15:11