concat two int values in postgresql

一个人想着一个人 提交于 2019-12-08 15:59:24

问题


I have 7 integer values (with 3,1,3,4,4,5,4 digits respectively) and I have to concatenate them to a single integer (i.e a 24 digit no.) . I tried to do it like this

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);  

But when I execute it I get error: bigint out of range . Is there any other better way to do it ?
thanks


回答1:


What about:

SELECT CAST(CAST(num1 AS text)||CAST(num2 AS text)||... AS numeric(24,0))

If you happen to have your IDs in some table, then you can do:

SELECT CAST(string_agg(CAST(num AS text), '') AS numeric(24,0)) FROM srctab;



回答2:


As I can concatenate a string to integer

SELECT REPLACE(STR(ISNULL(MAX(usuarioid) + 1, 1), 6), ' ', '0') FROM usuarios

usuarioid is string + 1



来源:https://stackoverflow.com/questions/11197000/concat-two-int-values-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!