在postgresql中连接两个int值

Kum*_*put 9 postgresql integer concatenation

我有7个整数值(分别有3,1,3,4,4,5,4位),我必须将它们连接成一个整数(即24位数字).我试着这样做

create or replace function gen_id(int,int,int,int,int,int,int) returns bigint as $$
declare
    id bigint;
begin
    id = $1 * 1000000000000000000000 + $2 * 100000000000000000000 + $3 * 100000000000000000 + $4 * 10000000000000 + $5 * 1000000000 + $6 * 10000 + $7;
    return id;
end;
$$ language plpgsql;

select * from gen_id(100,1,101,1000,1001,10001,1000);  
Run Code Online (Sandbox Code Playgroud)

但是当我执行它时,我得到错误:bigint超出范围.还有其他更好的方法吗?
谢谢

vye*_*rov 11

关于什么:

SELECT CAST(CAST(num1 AS text)||CAST(num2 AS text)||... AS numeric(24,0))
Run Code Online (Sandbox Code Playgroud)

如果您碰巧在某个表中有您的ID,那么您可以:

SELECT CAST(string_agg(CAST(num AS text), '') AS numeric(24,0)) FROM srctab;
Run Code Online (Sandbox Code Playgroud)

  • +1.另外,作为一个FYI(因为你正在使用PostgreSQL),你可以使用更方便的`::`运算符进行转换.换句话说,`CAST(num1 AS text)`与`num1 :: text`相同. (2认同)