PostgreSQL13.2でCOALESCE関数の使い方

環境
windows10 64bit
postgresql 13.2

書式
COALESCE( 対象文字列 , nullの場合に置換する文字 )
COALESCEを使用して、nullだった場合は置換します。

操作例1
判定する文字列はnullの場合

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
postgres=# select COALESCE(null , 'test') result;
result
--------
test
(1)
postgres=# select COALESCE(null , 'test') result; result -------- test (1 行)
postgres=# select COALESCE(null , 'test') result;
 result
--------
 test
(1 行)

操作例2
判定する文字列はnullではない場合

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
postgres=# select COALESCE('study' , 'demo') result;
result
--------
study
(1)
postgres=# select COALESCE('study' , 'demo') result; result -------- study (1 行)
postgres=# select COALESCE('study' , 'demo') result;
 result
--------
 study
(1 行)

 

PostgreSQL

Posted by arkgame