有没有办法在 postgresql 中将实际字符串转换为数字
我将记录存储在 db 中,以下是在每条记录中找到的唯一标识符的示例。
d895774d-7b91-4adc-bfba-f1264e5aae04
我想将此记录存储在另一个数据库中,但我想为每个交易/行生成另一个唯一编号
有什么方法可以将这个 d895774d-7b91-4adc-bfba-f1264e5aae04
转换为某个数字。
像这样的实际数字13693660123082308270370273012321321312321
select 'd895774d-7b91-4adc-bfba-f1264e5aae04' as id
答案 0 :(得分:4)
先将字符串转成单字符表(from
子句);
然后选择数字“原样”,a
为10
,b
为11
等(case
表达式);
最后聚合成一个字符串(string_agg
)
跳过 -
字符(where
子句)。
select
string_agg(case when c between '0' and '9' then c else (ascii(c) - 87)::text end, '')
from unnest(string_to_array('d895774d-7b91-4adc-bfba-f1264e5aae04', null)) c
where c <> '-';
结果:13895774137119141013121115111015126414510101404
select
td.date,
(
select string_agg(case when c between '0' and '9' then c else (ascii(c) - 87)::text end, '')
from unnest(string_to_array(td.id, null)) c
where c <> '-'
) as id
from table_d td;