在 postgresql 中连接两个 int 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11197000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
concat two int values in postgresql
提问by Kumar Rajput
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
我有 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 = * 1000000000000000000000 + * 100000000000000000000 + * 100000000000000000 + * 10000000000000 + * 1000000000 + * 10000 + ;
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
但是当我执行它时,我得到错误: bigint out of range 。有没有其他更好的方法来做到这一点?
谢谢
回答by vyegorov
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:
如果您碰巧在某个表中有您的 ID,那么您可以执行以下操作:
SELECT CAST(string_agg(CAST(num AS text), '') AS numeric(24,0)) FROM srctab;
回答by Pedro Ulises
As I can concatenate a string to integer
因为我可以将字符串连接为整数
SELECT REPLACE(STR(ISNULL(MAX(usuarioid) + 1, 1), 6), ' ', '0') FROM usuarios
usuarioid is string + 1
usuarioid 是字符串 + 1