PostgreSQL 文字列を置換する

PostgreSQL 文字列を置換する

PostgreSQLで、文字列を置換する手順を記述してます。

環境

  • OS Rocky Linux release 8.4 (Green Obsidian)
  • PostgreSQL 14.0
  • pgadmin 6.0

文字列を置換

文字列を置換する場合は、「replace」を使用します。

replace( 文字列,検索文字列,置換文字列 ) 

実際に、「world」を「mebee」に置換してみます。

select replace('hello world','world','mebee')

実行結果

置換されていることが確認できます。

指定した文字列は全て置換されます。

select replace('hello world','l','e')

実行結果

存在しない値

また、存在しないものを置換の対象に選択してもエラーにはなりません。

select replace('hello world','!!','mebee')

実行結果