

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

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

根據指定的開始位置傳回字串的子集。

如果輸入是字串，則提取的開始位置和字元數是以字元數為基礎，而不是位元組，所以多位元組字元視為單一字元。如果輸入是二進位運算式，則開始位置和提取的子字串是以位元組為基礎。您不能指定負長度，但可以指定負的開始位置。

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

```
SUBSTRING(charactestring FROM start_position [ FOR numbecharacters ] )
```

```
SUBSTRING(charactestring, start_position, numbecharacters )
```

```
SUBSTRING(binary_expression, start_byte, numbebytes )
```

```
SUBSTRING(binary_expression, start_byte )
```

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

 *特徵化*   
供進行搜尋的字串。非字元資料類型視為字串。

 *start\$1position*   
在字串內要開始擷取的位置，從 1 開始。*start\$1position* 以字元數為基礎，而不是位元組，所以多位元組字元視為單一字元。這可以是負數。

 *numbecharacters*   
要擷取的字元數 (子字串的長度)。*numbecharacters* 是根據字元數而非位元組數，因此多位元組字元會計為單一字元。這不能是負數。

 *start\$1byte*   
在二進位運算式內要開始擷取的位置，從 1 開始。這可以是負數。

 *numbebyte*   
要擷取的位元組數，即子字串的長度。此數字不可以是負數。

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

VARCHAR

## 字元字串的使用備註
<a name="SUBSTRING_usage_notes"></a>

下列範例傳回從第六個字元開始的四個字元的字串。

```
select substring('caterpillar',6,4);
substring
-----------
pill
(1 row)
```

如果 *start\$1position* \$1 *numbecharacters* 超過*字串*的長度，SUBSTRING 會從 *start\$1position* 傳回子字串，直到字串結束。例如：

```
select substring('caterpillar',6,8);
substring
-----------
pillar
(1 row)
```

如果 `start_position` 是負數或 0，SUBSTRING 函數會傳回從字串第一個字元開始且長度為 `start_position` \$1 `numbecharacters` -1 的子字串。例如：

```
select substring('caterpillar',-2,6);
substring
-----------
cat
(1 row)
```

如果 `start_position` \$1 `numbecharacters` -1 小於或等於零，SUBSTRING 會傳回空字串。例如：

```
select substring('caterpillar',-5,4);
substring
-----------

(1 row)
```

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

下列範例從 LISTING 資料表的 LISTTIME 字串中傳回月份：

```
select listid, listtime,
substring(listtime, 6, 2) as month
from listing
order by 1, 2, 3
limit 10;

 listid |      listtime       | month
--------+---------------------+-------
      1 | 2008-01-24 06:43:29 | 01
      2 | 2008-03-05 12:25:29 | 03
      3 | 2008-11-01 07:35:33 | 11
      4 | 2008-05-24 01:18:37 | 05
      5 | 2008-05-17 02:29:11 | 05
      6 | 2008-08-15 02:08:13 | 08
      7 | 2008-11-15 09:38:15 | 11
      8 | 2008-11-09 05:07:30 | 11
      9 | 2008-09-09 08:03:36 | 09
     10 | 2008-06-17 09:44:54 | 06
(10 rows)
```

下列範例同上，但使用 FROM...FOR 選項：

```
select listid, listtime,
substring(listtime from 6 for 2) as month
from listing
order by 1, 2, 3
limit 10;

 listid |      listtime       | month
--------+---------------------+-------
      1 | 2008-01-24 06:43:29 | 01
      2 | 2008-03-05 12:25:29 | 03
      3 | 2008-11-01 07:35:33 | 11
      4 | 2008-05-24 01:18:37 | 05
      5 | 2008-05-17 02:29:11 | 05
      6 | 2008-08-15 02:08:13 | 08
      7 | 2008-11-15 09:38:15 | 11
      8 | 2008-11-09 05:07:30 | 11
      9 | 2008-09-09 08:03:36 | 09
     10 | 2008-06-17 09:44:54 | 06
(10 rows)
```

如果字串可能包含雙位元組字元，則您無法使用 SUBSTRING 來肯定地擷取字串的字首，因為您需要根據位元組數來指定雙位元組字串的長度，而不是字元數。若要根據位元組長度來擷取字串的開頭部分，您可以將字串 CAST 成為 VARCHAR(*byte\$1length*) 以截斷字串，其中 *byte\$1length* 是所需的長度。下列範例從 `'Fourscore and seven'` 字串中擷取前 5 個位元組。

```
select cast('Fourscore and seven' as varchar(5));

varchar
-------
Fours
```

下列範例會傳回輸入字串 `Silva, Ana` 中最後一個空格之後出現的名字 `Ana`。

```
select reverse(substring(reverse('Silva, Ana'), 1, position(' ' IN reverse('Silva, Ana'))))

 reverse
-----------
 Ana
```