使用分隔符管道将数组转换为字符串 | 在 PostgreSQL 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2973705/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 22:33:48  来源:igfitidea点击:

Array to string conversion with delimiter pipe | in PostgreSQL

postgresql

提问by gvk

I need to form a string from a given array.

我需要从给定的数组中形成一个字符串。

Suppose array[1] = gvk, array[2] = gvk1 and array[3] = gvk2, then I need to get these values into a string like:

假设 array[1] = gvk、array[2] = gvk1 和 array[3] = gvk2,那么我需要将这些值放入一个字符串中,如:

Mystring = gvk | gvk1 | gvk2

我的字符串 = gvk | gvk1 | gvk2

回答by Peter Tillemans

I think you can use the array_to_stringfunction here :

我认为您可以在array_to_string这里使用该功能:

array_to_string(anyarray, text) --> text
concatenates array elements using supplied delimiter

array_to_string(anyarray, text) --> 文本
使用提供的分隔符连接数组元素

Example:

例子:

scalasb=> select array_to_string(ARRAY[1, 2, 3], '~^~');
 array_to_string 
-----------------
 1~^~2~^~3
(1 row)

scalasb=>

回答by Mark Baker

The MS SQL to POSTGRES Blogspothas a demonstration of how to implement a group_concat function in Postgres. It should be pretty straightforward to modify it to include a separator

MS SQL到POSTGRES Blogspot上有如何实现的Postgres一个GROUP_CONCAT功能的演示。修改它以包含分隔符应该非常简单