CONCAT

连接两个字符串并返回 varchar 数据类型。如果任一实参为 NULL,则 CONCAT 返回 NULL。

语法

CONCAT ('string‑expression1, string‑expression2)

行为类型

不可变

参数

string‑expression1, string‑expression2
要连接的值,可转换为字符串值的任何数据类型。

示例

以下示例使用名为 alphabet 的示例表,其中包含两个 VARCHAR 列:

=> CREATE TABLE alphabet (letter1 varchar(2), letter2 varchar(2));
CREATE TABLE
=> COPY alphabet FROM STDIN;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> A|B
>> C|D
>> \.
=> SELECT * FROM alphabet;
 letter1 | letter2
---------+---------
 C       | D
 A       | B
(2 rows)

用字符串连接第一列的内容:

=> SELECT CONCAT(letter1, ' is a letter') FROM alphabet;
    CONCAT
---------------
 A is a letter
 C is a letter
(2 rows)

连接两个嵌套 CONCAT 函数的输出:

=> SELECT CONCAT(CONCAT(letter1, ' and '), CONCAT(letter2, ' are both letters')) FROM alphabet;
          CONCAT
--------------------------
 C and D are both letters
 A and B are both letters
(2 rows)

连接日期和字符串:

=> SELECT current_date today;
   today
------------
 2021-12-10
(1 row)

=> SELECT CONCAT('2021-12-31'::date - current_date, ' days until end of year 2021');
             CONCAT
--------------------------------
 21 days until end of year 2021
(1 row)