concat_ws()

一个用于连接带有分隔符的值的函数

concat_ws() 是一个系统函数,用于将任意值连接成一个 text 字符串,并用分隔符(delimiter)连接。

concat_ws()PostgreSQL 9.1 中添加。

用法

concat_ws ( sep text , val1 any [, val2 any [, ...] ] ) → text

第一个参数是用于分隔符的字符串,可以是任何非 NULL 值。

要聚合查询中列的字符串值,请使用 string_agg()

变更历史

示例

concat_ws() 的基本用法示例

postgres=# SELECT concat_ws('|', 'foo', 'bar');
 concat_ws 
-----------
 foo|bar
(1 row)

NULL 值将被忽略

postgres=# SELECT concat_ws('|', 'foo', NULL, 'bar');
 concat_ws 
-----------
 foo|bar
(1 row)

如果所有提供的值都为 NULL,则返回一个空字符串。

postgres=# SELECT concat_ws('|', NULL, NULL) IS NULL;
 ?column? 
----------
 f
(1 row)

但是,NULL 分隔符会导致 NULL 结果。

postgres=# SELECT concat_ws(NULL, 'foo', 'bar') IS NULL;
 ?column? 
----------
 t
(1 row)

提供空字符串作为分隔符在功能上与使用 concat() 相同。

postgres=# SELECT concat_ws('', 'foo', 'bar'), concat('foo', 'bar');
 concat_ws | concat 
-----------+--------
 foobar    | foobar
(1 row)

分类

字符串操作, 系统函数

另请参阅

concat(), string_agg()

反馈

提交关于 "concat_ws()" 的任何评论、建议或更正请点击 这里