

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

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

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

所述 OCTETINDEX 函數傳回一個字串作為一個位元組數中的子字串的位置。

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

```
OCTETINDEX(substring, string)
```

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

 *substring*   
`CHAR` 字串、`VARCHAR` 字串或隱含評估為 `CHAR` 或 `VARCHAR` 類型的運算式。

 *string*   
`CHAR` 字串、`VARCHAR` 字串或隱含評估為 `CHAR` 或 `VARCHAR` 類型的運算式。

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

 INTEGER   
OCTETINDEX 函式會傳回與 *string* 中 *substring* 位置相對應的 `INTEGER` 值作為位元組數目，其中 *string* 中的第一個字元會計算為 1。如果 *string* 不包含多位元組字元，結果會等於 CHARINDEX 函數的結果。如果 *string* 不包含 *substring*，則函數會傳回 `0`。如果 *substring* 為空，則函數會傳回 `1`。

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

若要傳回字串 `q` 中子字串的位置`Amazon Redshift`，請使用下列範例。此範例會傳回 `0`，因為 *substring* 不在 *string* 中。

```
SELECT OCTETINDEX('q', 'Amazon Redshift');

+------------+
| octetindex |
+------------+
|          0 |
+------------+
```

若要傳回字串 中空子字串的位置`Amazon Redshift`，請使用下列範例。這個範例會傳回 `1`，因為 *substring* 是空的。

```
SELECT OCTETINDEX('', 'Amazon Redshift');

+------------+
| octetindex |
+------------+
|          1 |
+------------+
```

若要傳回字串 `Redshift` 中子字串的位置`Amazon Redshift`，請使用下列範例。這個範例會傳回 `8`，因為 *substring* 是從 *string* 的第八個位元組開始。

```
SELECT OCTETINDEX('Redshift', 'Amazon Redshift');

+------------+
| octetindex |
+------------+
|          8 |
+------------+
```

若要傳回字串 `Redshift` 中子字串的位置`Amazon Redshift`，請使用下列範例。這個範例會傳回 `21`，因為 *string* 的前六個字元是雙位元組字元。

```
SELECT OCTETINDEX('Redshift', 'Άμαζον Amazon Redshift');

+------------+
| octetindex |
+------------+
|         21 |
+------------+
```