1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

COALESCE関数で、欠損値にデフォルト値を設定しておく。

Posted at

COALESCE関数とは

引数を順番に評価し、引数内でNULLと評価されない最初の式の現在の値を返す。

(例)

  • select coalesce(null, '鈴木', null)
    →鈴木

  • select coalesce(カラム名, '') from テーブル名
    →指定したカラムでnullのフィールドがあった場合、そこに空白を挿入したうえでデータを抽出する。

  • select case when coalesce(カラム名, '') <> '' then True else False end
    →まず、指定したカラムでnullのフィールドがあった場合、空白を挿入する。
    そして、フィールドが空白ではない場合、「True」を格納し、空白の場合「False」を格納したうえでデータを抽出する。
    (指定したカラムのフィールドがnullまたは空文字ではなかった場合「True」を格納し、
    そうでなければ「False」にする。)

user_idがNULLであれば、guestuser_idが存在した場合はmembershipという値を格納するなどの活用方法がある。

1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?