LoginSignup
0
1

More than 5 years have passed since last update.

Postgresqlで文字列同士の結合をする際の注意

Last updated at Posted at 2018-01-01

NULLを含む文字列を結合すると全部NULLになるので注意

【現象】
columA = 111 columB = 2222 の場合、下記のように結合します。

SELECT columA || '-' || columB FROM tableA;

結果

111-2222

columA = 111 columB =  の場合、同じように結合しても結果が変わります。

結果

<NULL>

【対応方法】

SELECT columA || '-' || coalesce((columB, '') FROM tableA;

結果
111-

NULL文字が含まれる可能性のあるcolumBに対してcoalesce関数をかけることで対応することができます。
上記の場合、columBがNULLだった場合に空文字が返ります。

参考にさせて頂いたサイト:リンギオ

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1