> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/19/the-sql-language/functions/functions-enum.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 9.10. 列舉支援函式 \#

### 9.10. 列舉支援函式 [#](#functions-enum) <a href="#functions-enum" id="functions-enum"></a>

對於列舉型別（說明於[第 8.7 節](https://docs.postgresql.tw/19/the-sql-language/datatype/datatype-enum.md)），有幾個函式可以讓程式寫得更簡潔，而不必把列舉型別的特定值寫死在程式中。這些函式列於[表 9.35](#functions-enum-table)。範例假設有一個如下建立的列舉型別：

```

CREATE TYPE rainbow AS ENUM ('red', 'orange', 'yellow', 'green', 'blue', 'purple');
```

#### 表 9.35. 列舉支援函式 [#](#functions-enum-table) <a href="#functions-enum-table" id="functions-enum-table"></a>

| <p>函式</p><p>說明</p><p>範例</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>enum\_first</code> ( <code>anyenum</code> ) → <code>anyenum</code></p><p>回傳輸入列舉型別的第一個值。</p><p><code>enum\_first(null::rainbow)</code> → <code>red</code></p>                                                                                                                                                                                                                                                                                                                                                                   |
| <p><code>enum\_last</code> ( <code>anyenum</code> ) → <code>anyenum</code></p><p>回傳輸入列舉型別的最後一個值。</p><p><code>enum\_last(null::rainbow)</code> → <code>purple</code></p>                                                                                                                                                                                                                                                                                                                                                                 |
| <p><code>enum\_range</code> ( <code>anyenum</code> ) → <code>anyarray</code></p><p>以有序陣列的形式回傳輸入列舉型別的所有值。</p><p><code>enum\_range(null::rainbow)</code> → <code>{red,orange,yellow,​green,blue,purple}</code></p>                                                                                                                                                                                                                                                                                                                        |
| <p><code>enum\_range</code> ( <code>anyenum</code>, <code>anyenum</code> ) → <code>anyarray</code></p><p>以有序陣列的形式回傳兩個給定列舉值之間的範圍。這兩個值必須來自同一個列舉型別。如果第一個參數為 null，結果會從該列舉型別的第一個值開始。如果第二個參數為 null，結果會在該列舉型別的最後一個值結束。</p><p><code>enum\_range('orange'::rainbow, 'green'::rainbow)</code> → <code>{orange,yellow,green}</code></p><p><code>enum\_range(NULL, 'green'::rainbow)</code> → <code>{red,orange,​yellow,green}</code></p><p><code>enum\_range('orange'::rainbow, NULL)</code> → <code>{orange,yellow,green,​blue,purple}</code></p> |

<br>

請注意，除了雙引數形式的 `enum_range` 之外，這些函式都不理會傳給它們的具體值；它們只在意其宣告的資料型別。傳入 null 或該型別的特定值都可以，結果相同。比起像範例中那樣套用在寫死的型別名稱上，更常見的做法是將這些函式套用到資料表欄位或函式引數上。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/functions-enum.html)（原文版本：18.6；核對日期：2026-09-11）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.postgresql.tw/19/the-sql-language/functions/functions-enum.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
