PostgreSQL で文字列を結合(連結)するには、|| 演算子か concat() 関数を使います。違いは NULL の扱いで、|| は 1 つでも NULL があると結果が NULL になり、concat() は NULL を無視します。
文字列を結合する(|| と concat)
concat() は、3 つ以上の値もまとめて結合でき、数値などの文字列以外もそのまま渡せます。
appdb=# select 'abc' || 'def' as pipe, concat('abc', 'def', 1) as concat;
pipe | concat
--------+---------
abcdef | abcdef1
(1 row)
# || は、片方が文字列なら数値も結合できます
#
appdb=# select 'No.' || 1 as pipe_num;
pipe_num
----------
No.1
(1 row)
# 両方が数値だとエラーになります(concat() なら結合できます)
#
appdb=# select 1 || 2;
ERROR: operator does not exist: integer || integer
LINE 1: select 1 || 2;
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
appdb=#
NULL があるときの違い
|| で結合すると、1 つでも NULL が混じると結果も NULL になります。concat() は NULL を無視して結合します。見落としがちですが、concat() で NULL だけを結合しても、結果は NULL にはならず空文字になります。以下の検証用データで確かめます。
create table ex1 (
lno integer,
first_name varchar(20),
last_name varchar(20)
);
insert into ex1(lno,first_name,last_name) values(1,'太郎','山田');
insert into ex1(lno,first_name,last_name) values(2,null,'佐藤');
insert into ex1(lno,first_name,last_name) values(3,'花子',null);
insert into ex1(lno,first_name,last_name) values(4,null,null);
# 検証用データの確認
#
appdb=# \pset null '(null)'
Null display is "(null)".
appdb=# select * from ex1 order by 1;
lno | first_name | last_name
-----+------------+-----------
1 | 太郎 | 山田
2 | (null) | 佐藤
3 | 花子 | (null)
4 | (null) | (null)
(4 rows)
# concat()関数による結合は、いかなる場合も結合結果がnullになる事はありません
#
appdb=# select lno,concat(first_name,last_name) as full_name from ex1 order by 1;
lno | full_name
-----+-----------
1 | 太郎山田
2 | 佐藤
3 | 花子
4 | <--- nullだけの結合でもnullにはなりません
(4 rows)
# パイプによる結合は、NULLが含まれる場合結果はNULLになります。
#
appdb=# select lno,first_name||last_name as full_name from ex1 order by 1;
lno | full_name
-----+-----------
1 | 太郎山田
2 | (null)
3 | (null)
4 | (null)
(4 rows)
appdb=#
区切り文字を付けて結合する(concat_ws)
concat_ws() は、第一引数の区切り文字をはさんで結合します。NULL の項目は区切り文字ごと飛ばされるので、CSV のように項目の位置をそろえたい場合は、coalesce() で NULL を空文字に置き換えてから渡します。
# concat_ws()関数は、第一引数に指定する文字で、後続に続くパラメータ値を結合します。
# 以下の通り基本的にconcat()関数の動きと同じです。
#
appdb=# select lno,concat_ws('-',first_name,last_name) as full_name from ex1 order by 1;
lno | full_name
-----+-----------
1 | 太郎-山田
2 | 佐藤 <- 結合対象が存在しない場合、区切り文字も省略されます。
3 | 花子
4 |
(4 rows)
# 項目の位置をそろえたい場合(CSVなど)は、coalesce()でNULLを空文字にしてから渡します。
#
appdb=# select lno,concat_ws('-',coalesce(first_name,''),coalesce(last_name,'')) as full_name from ex1 order by 1;
lno | full_name
-----+-----------
1 | 太郎-山田
2 | -佐藤
3 | 花子-
4 | -
(4 rows)
appdb=#
複数行の値を 1 つに結合する(string_agg)
複数の行の値を 1 つの文字列にまとめたいときは、集約関数の string_agg() を使います。NULL の行は飛ばされます。配列にまとめる array_agg() などは「PostgreSQL 複数行の値を1行でまとめて表示する」にまとめています。
appdb=# select string_agg(last_name, ',' order by lno) as names from ex1;
names
-----------
山田,佐藤
(1 row)
appdb=#
生成列では concat() が使えない
concat() は IMMUTABLE(いつも同じ結果を返す)関数ではないため、生成列の式には使えません。生成列では、coalesce() で NULL を空文字に置き換えてから || で結合する形で代用します。
appdb=# create table ex2 (a text, b text,
appdb(# ab text generated always as (concat(a, b)) stored);
ERROR: generation expression is not immutable
# coalesce() と || で代用します
#
appdb=# create table ex3 (a text, b text,
appdb(# ab text generated always as (coalesce(a, '') || coalesce(b, '')) stored);
CREATE TABLE
appdb=#
Oracle との違い
- Oracle は
||でも NULL を無視して結合します。PostgreSQL の||は NULL になるので、移行するときは注意が必要です。 - Oracle では NULL 同士を結合すると NULL になります(Oracle は空文字を NULL として扱うため)。PostgreSQL の
concat()は空文字になります。 - Oracle の CONCAT は 2 つの値しか結合できません。PostgreSQL の
concat()は一度に複数の値を結合でき、とても便利です。

