メインコンテンツまでスキップ
に最終更新

SUB_REPLACE

説明​

sub_replace 関数は、文字列内の部分文字列を置換します。置換対象の部分文字列と、その置換に使用する文字列を指定できます。start から長さ len の範囲にある str の部分文字列を new_str に置き換えた新しい文字列を返します。start または len が負の整数の場合は NULL を返します。len のデフォルト値は new_str の長さです。

構文​

sub_replace(<str>, <new_str>, [ ,<start> [ , <len> ] ])

パラメータ​

パラメータ説明
<str>置換対象の文字列
<new_str>指定した部分文字列の置換に使用する文字列
<start>start は、文字列内で置換処理を開始する位置を指定します。省略可能です
<len>len は省略可能で、置換する部分文字列の長さを指定します

戻り値​

置換後の文字列を返します。

例​

  1. 基本的な使い方:位置と長さを指定して置換
SELECT sub_replace('doris', '***', 1, 2);
+-----------------------------------+
| sub_replace('doris', '***', 1, 2) |
+-----------------------------------+
| d***is |
+-----------------------------------+
  1. デフォルトの長さを使用して置換
SELECT sub_replace('hello', 'Hi', 0);
+--------------------------------+
| sub_replace('hello', 'Hi', 0) |
+--------------------------------+
| Hillo |
+--------------------------------+
  1. 負の引数を指定すると NULL を返す
SELECT sub_replace('hello', 'Hi', -1, 2);
+------------------------------------+
| sub_replace('hello', 'Hi', -1, 2) |
+------------------------------------+
| NULL |
+------------------------------------+
  1. NULL 値の処理
SELECT sub_replace(NULL, 'new', 0, 3);
+-------------------------------------+
| sub_replace(NULL, 'new', 0, 3) |
+-------------------------------------+
| NULL |
+-------------------------------------+
  1. UTF-8 文字列
SELECT sub_replace('doris', 'ṛìḍḍ', 1, 2);
+-------------------------------------------+
| sub_replace('doris', 'ṛìḍḍ', 1, 2) |
+-------------------------------------------+
| dṛìḍḍis |
+-------------------------------------------+
  1. 開始位置が文字列の長さを超える場合
SELECT sub_replace('hello', 'Hi', 9, 2);
+----------------------------------+
| sub_replace('hello', 'Hi', 9, 2) |
+----------------------------------+
| NULL |
+----------------------------------+
  1. 指定した置換範囲が文字列の末尾を超える場合
SELECT sub_replace('hello', 'Hi', 1, 9);
+----------------------------------+
| sub_replace('hello', 'Hi', 1, 9) |
+----------------------------------+
| hHi |
+----------------------------------+

キーワード​

SUB_REPLACE, REPLACE