PostgreSQL 13にcoalesce関数で文字列とnullを結合する

2022年1月8日

環境
Windows10 64 bit
PostgreSQL 13.2

書式
coalesce(対象文字列 , 左の文字列がnullの時ここで指定した文字を返す)
一つ目の引数がnullの時、二つ目の引数を返します

使用例1
nullの時’study’で返します

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

使用例2
結合する文字列1: nullの時’study’で返します
結合する文字列2:nullの時’skill’で返します
結合する文字列それぞれにcoalesceを付けて結合します

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

 

PostgreSQL

Posted by arkgame