

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# REPLACE 函數
<a name="REPLACE"></a>

以其他指定的字元取代一組字元在現有字串內出現的所有地方。

REPLACE 類似於 [TRANSLATE 函數](TRANSLATE.md) 和 [REGEXP\$1REPLACE 函數](REGEXP_REPLACE.md)，但 TRANSLATE 會進行多次單一字元替換，REGEXP\$1REPLACE 可讓您在字串中搜尋規則表達式模式，而 REPLACE 會將一整個字串替換成另一個字串。

## 語法
<a name="REPLACE-synopsis"></a>

```
REPLACE(string1, old_chars, new_chars)
```

## 引數
<a name="REPLACE-arguments"></a>

 *string*   
要搜尋的 CHAR 或 VARCHAR 字串 

 *old\$1chars*   
要取代的 CHAR 或 VARCHAR 字串。

 *new\$1chars*   
新的 CHAR 或 VARCHAR 字串，用來取代 *old\$1string*。

## 傳回類型
<a name="REPLACE-return-type"></a>

VARCHAR

如果 *old\$1chars* 或 *new\$1chars* 為 NULL，則結果為 NULL。

## 範例
<a name="REPLACE-examples"></a>

下列範例將 CATGROUP 欄位中的字串 `Shows` 轉換為 `Theatre`：

```
select catid, catgroup,
replace(catgroup, 'Shows', 'Theatre')
from category
order by 1,2,3;

 catid | catgroup | replace
-------+----------+----------
     1 | Sports   | Sports
     2 | Sports   | Sports
     3 | Sports   | Sports
     4 | Sports   | Sports
     5 | Sports   | Sports
     6 | Shows    | Theatre
     7 | Shows    | Theatre
     8 | Shows    | Theatre
     9 | Concerts | Concerts
    10 | Concerts | Concerts
    11 | Concerts | Concerts
(11 rows)
```