

 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/)。

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

# LIKE
<a name="r_patternmatching_condition_like"></a>

LIKE 運算子會利用模式 (此模式使用萬用字元 % (百分比) 和 \_ (底線)) 來比較字串表達式 (例如資料欄的名稱)。LIKE 模式比對的範圍一律涵蓋整個字串。若要比對字串中任意位置的序列，模式必須以百分比符號開頭和結尾。

LIKE 區分大小寫；ILIKE 不區分大小寫。

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

```
expression [ NOT ] LIKE | ILIKE pattern [ ESCAPE 'escape_char' ]
```

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

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

LIKE \| ILIKE   
LIKE 會進行區分大小寫的模式比對。ILIKE 會針對單位元組 UTF-8 (ASCII) 字元，進行不區分大小寫的模式比對。若要對多位元組字元執行不區分大小寫的模式比對，請在具有 LIKE 條件的 *expression* 和 *pattern* 上使用 [LOWER](r_LOWER.md) 函數。  
不同於比較述詞，例如 = 和 <>，LIKE 和 ILIKE 述詞並未隱含忽略結尾空格。若要忽略結尾空格，請 RTRIM 或將 CHAR 資料欄明確轉換為 VARCHAR。  
`~~` 運算子相當於 LIKE，而 `~~*` 相當於 ILIKE。此外，`!~~` 和 `!~~*` 運算子相當於 NOT LIKE 和 NOT ILIKE。

 *pattern*   
有效的 UTF-8 字元表達式，包含要比對的模式。

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

如果 *pattern* 未包含任何中繼字元，則模式只代表字串本身，此時 LIKE 的功用如同等於運算子。

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

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


| 運算子  | Description  | 
| --- | --- | 
| %  | 比對任何 0 的序列或更多字元。 | 
| \_ | 比對任一個單一字元。 | 

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

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


| 表達式  | 傳回值  | 
| --- | --- | 
| 'abc' LIKE 'abc' | True | 
| 'abc' LIKE 'a%' | True | 
| 'abc' LIKE '\_B\_' | False | 
| 'abc' ILIKE '\_B\_' | True | 
| 'abc' LIKE 'c%' | False | 

下列範例會找出名稱以「E」開頭的所有城市：

```
select distinct city from users
where city like 'E%' order by city;
city
---------------
East Hartford
East Lansing
East Rutherford
East St. Louis
Easthampton
Easton
Eatontown
Eau Claire
...
```

下列範例會找出姓氏中包含「ten」的使用者：

```
select distinct lastname from users
where lastname like '%ten%' order by lastname;
lastname
-------------
Christensen
Wooten
...
```

以下範例示範如何比對多個模式。

```
select distinct lastname from tickit.users
where lastname like 'Chris%' or lastname like '%Wooten' order by lastname;
lastname
-------------
Christensen
Christian
Wooten
...
```

下列範例會找出名稱中第 3 個和第 4 個字元為「ea」的城市。此指令使用 ILIKE 來示範不區分大小寫：

```
select distinct city from users where city ilike '__EA%' order by city;
city
-------------
Brea
Clearwater
Great Falls
Ocean City
Olean
Wheaton
(6 rows)
```

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

```
select tablename, "column" from pg_table_def 
where "column" like '%start\\_%'
limit 5;

     tablename     |    column
-------------------+---------------
 stl_s3client      | start_time
 stl_tr_conflict   | xact_start_ts
 stl_undone        | undo_start_ts
 stl_unload_log    | start_time
 stl_vacuum_detail | start_row
(5 rows)
```

下列的範例將「^」指定為逸出字元，然後使用該逸出字元來搜尋包含「\_」的字串 (文字 `start` 後跟底線 `_`)：

```
select tablename, "column" from pg_table_def 
where "column" like '%start^_%' escape '^' 
limit 5;

     tablename     |    column
-------------------+---------------
 stl_s3client      | start_time
 stl_tr_conflict   | xact_start_ts
 stl_undone        | undo_start_ts
 stl_unload_log    | start_time
 stl_vacuum_detail | start_row
(5 rows)
```

下列範例會使用 `~~*` 運算子執行不區分大小寫 (ILIKE)，搜尋以「Ag」開頭的城市。

```
select distinct city from users where city ~~* 'Ag%' order by city;
                   
city
------------
Agat	
Agawam	
Agoura Hills	
Aguadilla
```