在实际使用postgresql过程中,收集和整理了一些常用的函数,分享给大家。
字符串拼接
select concat(1, 2, '3', array [4,5]);
-- 123{4,5}
行聚合, 多行合并为一行, 拼接为字符串
select string_agg(column1,',') from ( values ('1'),('2'),('3') ) t;
-- 1,2,3
select string_agg(concat(column1,'-',column2),',') from ( values ('1','2'),('3','4'),('5','6') ) t;
-- 1-2,3-4,5-6
select string_agg(column1,','),string_agg(column2,',') from ( values ('1','2'),('3','4'),('5','6') ) t;
-- 1,3,5 2,4,6
按指定字符切割
select string_to_array('一,剑,下,天,山',''), pg_typeof(string_to_array('一,剑,下,天,山',''))
-- {一,剑,下,天,山} text[]
返回第N个片段
select split_part('ordno-#-orddt-#-ordamt', '-#-', 3);
-- ordamt
字符串长度
selec