

 Amazon Redshift 將不再支援從修補程式 198 開始建立新的 Python UDFs。現有 Python UDF 將繼續正常運作至 2026 年 6 月 30 日。如需詳細資訊，請參閱[部落格文章](https://aws.amazon.com/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

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

# SIMILAR TO
<a name="pattern-matching-conditions-similar-to"></a>

SIMILAR TO 運算子會利用 SQL 標準規則表達式的模式，來比對字串表達式 (例如資料欄的名稱)。SQL 標準規則表達式的模式可包含一組模式比對中繼字元，其中包括 [LIKE](r_patternmatching_condition_like.md) 運算子所支援的兩個字元。

SIMILAR TO 運算子只會在其模式符合整個字串時傳回 true，不像 POSIX 規則表達式，其模式可以符合字串的任何部分。

SIMILAR TO 會進行區分大小寫的比對。

**注意**  
使用 SIMILAR TO 進行的規則表達式比對，其運算成本非常昂貴。我們建議盡可能使用 LIKE，尤其是在處理極為龐大的列數時。例如，下列的查詢在功能上相同，但相較於使用規則運算式的查詢，使用 LIKE 的查詢，其執行速度快上好幾倍：  

```
select count(*) from event where eventname SIMILAR TO '%(Ring|Die)%'; 
select count(*) from event where eventname LIKE '%Ring%' OR eventname LIKE '%Die%';
```

## 語法
<a name="pattern-matching-conditions-similar-to-synopsis"></a>

```
expression [ NOT ] SIMILAR TO pattern [ ESCAPE 'escape_char' ]
```

## 引數
<a name="pattern-matching-conditions-similar-to-arguments"></a>

 *表達式*   
有效的 UTF-8 字元表達式，例如資料欄的名稱。

SIMILAR TO  
SIMILAR TO 會針對 *expression* 中的整個字串，進行區分大小寫的模式比對。

 *pattern*   
有效的 UTF-8 字元表達式，代表 SQL 標準規則表達式的模式。

 *escape\$1char*   
字元表達式，將會用來逸出模式中的中繼字元。預設值為兩個反斜線 (「\$1\$1」)。

如果*模式*未包含任何中繼字元，則模式只代表字串本身。

兩個字元表達式都可以是 CHAR 或 VARCHAR 資料類型。如果不同，Amazon Redshift 會將 *pattern* 轉換為 *expression* 的資料類型。

SIMILAR TO 支援下列的模式比對中繼字元：

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/redshift/latest/dg/pattern-matching-conditions-similar-to.html)

## 範例
<a name="pattern-matching-conditions-similar-to-examples"></a>

下表顯示範例，示範使用 SIMILAR TO 進行的模式比對：

[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/redshift/latest/dg/pattern-matching-conditions-similar-to.html)

下列範例會找出名稱中包含「E」或「H」的城市：

```
SELECT DISTINCT city FROM users
WHERE city SIMILAR TO '%E%|%H%' ORDER BY city LIMIT 5;

      city
-----------------
 Agoura Hills
 Auburn Hills
 Benton Harbor
 Beverly Hills
 Chicago Heights
```

下列的範例使用預設的逸出字串 (「`\\`」)，來搜尋包含「`_`」的字串：

```
SELECT tablename, "column" FROM pg_table_def
WHERE "column" SIMILAR TO '%start\\_%'
ORDER BY tablename, "column" LIMIT 5;

        tablename         |       column
--------------------------+---------------------
 stcs_abort_idle          | idle_start_time
 stcs_abort_idle          | txn_start_time
 stcs_analyze_compression | start_time
 stcs_auto_worker_levels  | start_level
 stcs_auto_worker_levels  | start_wlm_occupancy
```

下列的範例將「`^`」指定為逸出字元，然後使用該逸出字元來搜尋包含「`_`」的字串：

```
SELECT tablename, "column" FROM pg_table_def
WHERE "column" SIMILAR TO '%start^_%' ESCAPE '^'
ORDER BY tablename, "column" LIMIT 5;

        tablename         |       column
--------------------------+---------------------
 stcs_abort_idle          | idle_start_time
 stcs_abort_idle          | txn_start_time
 stcs_analyze_compression | start_time
 stcs_auto_worker_levels  | start_level
 stcs_auto_worker_levels  | start_wlm_occupancy
```