如何在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
相关产品推荐
相关产品推荐

