メインコンテンツまでスキップ
バージョン: 26.x

SUB_REPLACE

説明

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

構文

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

パラメータ

Parameterデスクリプション
<str>置換が実行される対象の文字列
<new_str>指定された部分文字列を置き換える文字列
<start>startは置換操作が開始される位置で、文字列内のどの位置から置換を開始するかを示します
<len>lenは置換される部分文字列の長さを指定するオプションパラメータです

Return Value

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

Examples

  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 |
+----------------------------------+

Keywords

SUB_REPLACE, REPLACE