PostgreSQL 13に文字列の改行コードを置換する

環境
Windows10 64 bit
PostgreSQL 13.2

書式
select REGEXP_REPLACE('改行コートが含まれる文字列’,’\r|\n|\r\n’, '置換文字’,’g’);

使用例1
改行コードを別の文字に置換します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
postgres=# select REGEXP_REPLACE('AA
postgres'# BB
postgres'# CC','\r|\n|\r\n', 'study','g');
regexp_replace
------------------
AAstudyBBstudyCC
(1)
postgres=# select REGEXP_REPLACE('AA postgres'# BB postgres'# CC','\r|\n|\r\n', 'study','g'); regexp_replace ------------------ AAstudyBBstudyCC (1 行)
postgres=# select REGEXP_REPLACE('AA
postgres'# BB
postgres'# CC','\r|\n|\r\n', 'study','g');
  regexp_replace
------------------
 AAstudyBBstudyCC
(1 行)

使用例2
複数行の文の改行を置換して、1行に変換します

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
postgres=# select REGEXP_REPLACE('AA
postgres'# BB
postgres'# CC','\r|\n|\r\n', '','g') result;
result
--------
AABBCC
(1)
postgres=# select REGEXP_REPLACE('AA postgres'# BB postgres'# CC','\r|\n|\r\n', '','g') result; result -------- AABBCC (1 行)
postgres=# select REGEXP_REPLACE('AA
postgres'# BB
postgres'# CC','\r|\n|\r\n', '','g') result;
 result
--------
 AABBCC
(1 行)

 

PostgreSQL

Posted by arkgame