PostgreSQL13 split_partで区切り位置からの順番を指定して文字列を取得する

環境
Windows 10 Home 64bit
PostgreSQL 13.2

書式
split_part(string text, delimiter text, field int)
stringをdelimiterで分割し、その結果から(1から始まるように数える)指定したフィールドを返します。

使用例1
SQL構文

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
select split_part('stu,dy,skill', ',', 1) result;
select split_part('stu,dy,skill', ',', 1) result;
select split_part('stu,dy,skill', ',', 1) result;

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
postgres=# select split_part('stu,dy,skill', ',', 1) result;
 result
----------
stu
(1)
postgres=# select split_part('stu,dy,skill', ',', 1) result;  result ---------- stu (1 行)
postgres=# select split_part('stu,dy,skill', ',', 1) result;
  result
----------
 stu
(1 行)

使用例2
SQL構文

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
select split_part('stu~@~dy~@~skill', '~@~', 2) result;
select split_part('stu~@~dy~@~skill', '~@~', 2) result;
select split_part('stu~@~dy~@~skill', '~@~', 2) result;

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
postgres=# select split_part('stu~@~dy~@~skill', '~@~', 2) result;
result
--------
dy
(1)
postgres=# select split_part('stu~@~dy~@~skill', '~@~', 2) result; result -------- dy (1 行)
postgres=# select split_part('stu~@~dy~@~skill', '~@~', 2) result;
 result
--------
 dy
(1 行)

 

PostgreSQL

Posted by arkgame