---
schema_version: "1"
id: ae2c95a8a7f42197292b77cd803d538a0ae5f4c9c6cd6fda32015e0c89a02725
title: 將欄位轉換為日期欄位
collection: tableau
version: current
locale: zh-tw
portal_url: https://docs.salesforce.com/tableau/current/zh-tw/public/desktop/data_dateparse.md
canonical_url: https://help.tableau.com/current/public/desktop/zh-tw/data_dateparse.htm
source_site: help.tableau.com
content_type: documentation
content_hash: 165a05b70eacf680aa317accad66deda5cd1cb6ab1035087fd675705b51deffd
description: Convert a text or numeric field to a date using data type changes and the DATEPARSE function.
---
[跳至主要內容](#main-content)

# 將欄位轉換為日期欄位

每種資料庫存儲日期欄位的方式都略有不同。Tableau 會盡力對日期欄位進行解讀，但有時匯入 Tableau 的欄位將會是文字字串或數位欄位。如果發生這種情況，我們可以透過幾個步驟來解決問題。

> [!TIP]
> 
> **提示**：以下資訊會介紹如何將欄位的資料類型變更為日期。若要設定現有日期欄位的格式並控制其在視覺效果中的顯示方式，請參閱[自訂日期格式](https://help.tableau.com/current/pro/desktop/zh-tw/dates_custom_date_formats.htm)。

## 驗證日期欄位

日期欄位可能顯示為字串，並帶有 Abc 資料類型圖示 ![文字或字串值。](/tableau/current/zh-tw/public/desktop/zh-tw/Img/symbol_text_row.png)。

當日期被解讀為字串時，您在使用日期欄位時所能享受到的功能和便捷性（例如下鑽、使用日期計算以及在連續和離散度量之間切換）將蕩然無存。

如果日期解讀不正確，請按循序執行以下步驟：

1. [變更欄位的資料類型](#type)
2. [使用 DATEPARSE 函數建立計算](#dateparse)
3. [使用 DATE 函數建立計算](#date)

## 變更欄位的資料類型

解決日期欄位解讀問題的第一步是確保資料類型設定為 \[日期\] 或 \[日期和時間\] 。

1. 在資料來源或資料窗格中，按一下日期欄位的資料類型圖示，然後將資料類型變更為日期或日期和時間
    
    ![下拉式功能表顯示欄位的資料類型，其中醒目提示日期和時間選項。](/tableau/current/zh-tw/public/desktop/zh-tw/Img/dateparse_type.png)
2. 檢查檢視或 \[資料來源\] 窗格中的資料。如果看到許多 Null 值，請將資料類型恢復為 \[字串\] ，並繼續[使用 DATEPARSE 函數建立計算](#dateparse)來糾正問題。
    
    ![顯示在「資料來源」畫面中的 Null 值。](/tableau/current/zh-tw/public/desktop/zh-tw/Img/dateparse_example_3.png "存在 Null 值。")

## 使用 DATEPARSE 函數建立計算

日期採用一系列近乎無限的格式存儲。某些日期欄位將年放在月之前，其他一些日期欄位用句點分隔日期的各個部分，還有一些則使用各種格式的組合。當 Tableau 無法解讀日期欄位時，原因可能是無法轉換特定格式。

DATEPARSE 函數使您能夠清晰地定義欄位的哪些部分對應於日期的哪些部分。本質上，您是在建立一個 Tableau 可用於將字串轉換為日期欄位的對應。這種對應稱為格式。

**附註：**此函數可透過以下連接器獲得：非舊版 Excel 和文字檔連線、Amazon EMR Hadoop Hive、Cloudera Hadoop、Google 試算表、Hortonworks Hadoop Hive、MapR Hadoop Hive、MySQL、Oracle、PostgreSQL 以及 Tableau 資料擷取。有些格式可能並非適用於所有連線。  
  
Hive 變體上不支援 DATEPARSE。僅支援 Denodo、Drill 和 Snowflake。

### 建立 DATEPARSE 計算

1. 檢查日期欄位的當前格式。記下月、年、小時和天在欄位中的顯示位置。DATEPARSE 函數需要此資訊。
    
    <table style="width: 100%;"><colgroup><col><col></colgroup><tbody><tr><td><img src="https://help.tableau.com/current/public/desktop/zh-tw/Img/dateparse_example4.png" alt="日期剖析公式，其中公式各部分標示為 A、B、C 和 D。"></td><td><p><span class="uicontrol">A</span>. 當月日期</p><p><span class="uicontrol">B</span>. 月份</p><p><span class="uicontrol">C</span>. 年份</p><p><span class="uicontrol">D</span>. 小時、分鐘、秒、毫秒、期間</p></td></tr></tbody></table>
2. 在 \[維度\] 窗格中以右鍵按一下日期欄位，並選取 \[建立\] \> \[計算欄位\] 。
3. 在對話方塊中，編寫 DATEPARSE 函數。DATEPARSE 函數包含兩個部分：格式和字串。字串是您要轉換的欄位，其必須為字串資料類型 ![文字或字串值。](/tableau/current/zh-tw/public/desktop/zh-tw/Img/symbol_text_row.png)。
    
    <table class="table--plain" style="width: 100%;"><colgroup><col style="width: 569px;"><col></colgroup><tbody><tr><td><img src="https://help.tableau.com/current/public/desktop/zh-tw/Img/dateparse_example_5.png" alt="資料剖析公式，其中公式各部分標示為 A 和 B。"></td><td><p><span class="uicontrol">A</span>. 格式</p><p><span class="uicontrol">B</span>. 字串</p></td></tr></tbody></table>
    
    格式是 Tableau 用於將字串解讀為日期的指引。日期或時間字串的每個部分都有一個相符符號，如下表中所示。格式必須完全複製日期的顯示方式。舉例來說，如果在字串包含 2 位元數年份代碼（ \[26\] ）時使用單一年份代碼（ \[Y\] ），則計算可能返回 null 值。
    
    附註：格式要完全複製顯示的字串的例外情況是，日期的長格式部分（ \[September\] ）可使用四個符號設定格式（ \[September" = "MMMM\] ）。
    
    格式必須包括所有空格、連字號和其他非字母數位記號才能正確解讀字串。
    
    ### 日期欄位符號
    
    <table class="table-plain"><colgroup><col class="Column-Column1"><col class="Column-Column1"><col class="Column-Column1"></colgroup><tbody><tr class="Body-Body1"><th class="BodyE-Column1-Body1" style="font-weight: bold;">日期部分</th><th class="BodyE-Column1-Body1" style="font-weight: bold;">符號</th><th class="BodyE-Column1-Body1" style="font-weight: bold;">範例輸出</th></tr><tr class="Body-Body2"><td class="BodyE-Column1-Body2">年</td><td class="BodyE-Column1-Body2"><p>yy</p><p>y, yyyy</p><p>Y</p></td><td class="BodyE-Column1-Body2"><p>26</p><p>2026</p><p>2026（用於基於「年中的週」的行事曆）</p></td></tr><tr class="Body-Body1"><td class="BodyE-Column1-Body1">年中的季度</td><td class="BodyE-Column1-Body1"><p>Q</p><p>QQ</p><p>QQQ</p><p>QQQQ</p></td><td class="BodyE-Column1-Body1"><p>2</p><p>02</p><p>Q2</p><p>第二季</p></td></tr><tr class="Body-Body1"><td class="BodyE-Column1-Body1">月</td><td class="BodyE-Column1-Body1"><p>M</p><p>MM</p><p>MMM</p><p>MMMM</p></td><td class="BodyE-Column1-Body1"><p>9</p><p>09</p><p>9 月</p><p>九月</p></td></tr><tr class="Body-Body2"><td class="BodyE-Column1-Body2">年中的週</td><td class="BodyE-Column1-Body2">w, ww</td><td class="BodyE-Column1-Body2">47</td></tr><tr class="Body-Body2"><td class="BodyE-Column1-Body2">週中的天</td><td class="BodyE-Column1-Body2"><p>E, EE, EEE</p><p>EEEE</p><p>EEEEE</p></td><td class="BodyE-Column1-Body2"><p>一</p><p>週一</p><p>M</p></td></tr><tr class="Body-Body1"><td class="BodyE-Column1-Body1">月中的天</td><td class="BodyE-Column1-Body1"><p>d</p><p>dd</p></td><td class="BodyE-Column1-Body1"><p>1, 15</p><p>01, 15</p></td></tr><tr class="Body-Body2"><td class="BodyE-Column1-Body2">年中的天 (1-365)</td><td class="BodyE-Column1-Body2">D</td><td class="BodyE-Column1-Body2">23, 143</td></tr><tr class="Body-Body2"><td class="BodyE-Column1-Body2"><p>小時 (1-12)</p></td><td class="BodyE-Column1-Body2"><p>h</p><p>hh</p></td><td class="BodyE-Column1-Body2"><p>8</p><p>08</p></td></tr><tr class="Body-Body2"><td class="BodyE-Column1-Body2">小時 (0-23)</td><td class="BodyE-Column1-Body2"><p>H</p><p>HH</p></td><td class="BodyE-Column1-Body2"><p>8</p><p>08</p></td></tr><tr class="Body-Body1"><td class="BodyE-Column1-Body1">分鐘</td><td class="BodyE-Column1-Body1"><p>m</p><p>mm</p></td><td class="BodyE-Column1-Body1"><p>8</p><p>08</p></td></tr><tr class="Body-Body2"><td class="BodyE-Column1-Body2">分中的秒</td><td class="BodyE-Column1-Body2"><p>s</p><p>ss</p></td><td class="BodyE-Column1-Body2"><p>5</p><p>05</p></td></tr><tr class="Body-Body1"><td class="BodyE-Column1-Body1">小數秒</td><td class="BodyE-Column1-Body1"><p>S</p><p>SS</p><p>SSS</p><p>SSSS</p></td><td class="BodyE-Column1-Body1"><p>2</p><p>23</p><p>235</p><p>2350</p></td></tr><tr class="Body-Body2"><td class="BodyB-Column1-Body2">天中的毫秒</td><td class="BodyB-Column1-Body2">A</td><td class="BodyB-Column1-Body2">34532</td></tr><tr class="Body-Body1"><td class="BodyE-Column1-Body1">期間（上午/下午標記）</td><td class="BodyE-Column1-Body1">a, aa, aaa</td><td class="BodyE-Column1-Body1">AM, am, PM</td></tr><tr class="Body-Body2"><td class="BodyE-Column1-Body2">時區</td><td class="BodyE-Column1-Body2"><p>z, zz, zzz</p><p>zzzz</p><p>Z, ZZ, ZZZ</p><p>ZZZZ</p></td><td class="BodyE-Column1-Body2"><p>PDT</p><p>太平洋日光節約時間</p><p>-0800</p><p>PST-08:00</p></td></tr></tbody></table>
    
    有關日期符號的更完整清單，請參閱國際統一碼部件頁面的上[日期和時間格式設定概觀](https://unicode-org.github.io/icu/userguide/format_parse/datetime/)。
    
    **附註：** 並非所有資料庫和檔案類型都支援某些日期格式。
4. 依據 Tableau 中顯示的字串檢查您的格式。如果符號和格式設定正確無誤，請選取 \[確定\] 建立新計算欄位。計算欄位將充當檢視中的日期欄位。
    
    ![DATEPARSE 函數的圖表。](/tableau/current/zh-tw/public/desktop/zh-tw/Img/dateparse_example_6.png "將格式與字串進行比較。")

### Hyper 擷取

對於 .hyper 擷取資料來源，符號是由 Unicode 協會定義的。有關詳情，請轉到 [擷取資料來源 DATEPARSE 函數的格式語法](https://help.tableau.com/current/public/desktop/zh-tw/dates_custom_date_formats.htm#dateparse)，檢視可與 .hyper 擷取中的 DATEPARSE 函數結合使用的欄位類型和符號子集。

### 地區設定注意事項

DATEPARSE 函數依賴於電腦設定指定的地區設定來進行解釋，然後顯示要轉換的字串。更具體地說，地區設定將影響特定格式是否可識別。這意味著如果地區設定不支援某種格式，那麼您可能會看到 Null 值或者不會返回值。例如，假設資料中有以下字串：

`12Sep2026:9:8:8.6546`

根據地區設定，DATEPARSE 函數為此字串返回的值也有所不同。在英語地區設定中，您將獲取一個特定值，但對於日文地區設定，您將不會獲取任何值。在這種情況下，由於日文地區設定無法辨識「Sep」，因此不會傳回任何值。

**日期和時間值**

| 英語地區設定 | 日文地區設定 |
| --- | --- |
| #9/12/2026 9:08:09 AM# | \- |

如同所有日期一樣，在您使用了 DATEPARSE 函數將字串轉換為日期時間類型後，預設情況下 Tableau 將以您的地區設定的預設格式顯示日期時間值。如果地區設定發生變化，則 DATEPARSE 函數的結果可能會以不同的格式顯示新的日期時間值。

## 使用 DATE 函數建立計算

如果 DATEPARSE 函數不適用於您正在處理的資料，或者您要轉換的欄位是數位資料類型，則可以改用 DATE 函數。

DATE 函數會將數位、字串或日期運算式轉換為日期類型。建立一個使用 DATE 函數的計算時，Tableau 會在 Tableau 資料來源中建立一個新欄位，該欄位允許您與作為日期的日期資料進行交互。若要使用 DATE 函數利用數位、字串或日期運算式成功組建日期值，Tableau 需要能夠將字串的元件解釋為日期部分。識別日期的元件後，Tableau 會使用電腦地區設定確定日期的預設格式。

例如，假設您正在使用的表包含一個稱為 \[原始日期\] 的日期列。  \[原始日期\] 列是字串類型。

- **原始日期**：03Jan2026、05Jan2026、07Mar2026、19Mar2026、30Apr2026

在此情況下，您可以建立一個稱為 \[新日期\] 的計算欄位，該欄位使用 DATE 函數中的運算式將 \[原始日期\] 欄位中的字串值轉換為日期值。

`DATE (LEFT([Original Date], 2) + "/" + MID([Original Date],3,3) + "/" + RIGHT([Original Date],4))`

對於此範例，日期運算式由用於確定日元件的 LEFT 函數、用於確定月元件的 MID 函數以及用於確定年元件的 RIGHT 函數組成。

\[新日期\] 計算會組建以下列：

- **新日期**：1/3/2026、1/5/2026、3/7/2026、3/19/2026、4/30/2026

在此範例中，新日期值基於英語地區設定和預設格式。

## 另請參閱

[自訂日期格式](https://help.tableau.com/current/public/desktop/zh-tw/dates_custom_date_formats.htm)

[使用 ISO-8601 週和年份設定日期格式](https://help.tableau.com/current/public/desktop/zh-tw/dates_custom_date_formats_week.htm)

[日期函式](https://help.tableau.com/current/public/desktop/zh-tw/functions_functions_date.htm)

[所有函數（按類別）：日期](https://help.tableau.com/current/public/desktop/zh-tw/functions_all_categories.htm#Date)

[STR() 函數忽略預設日期和數位格式設定](http://kb.tableau.com/articles/Issue/str-function-ignores-date-and-number-formatting?lang=zh-tw)（Tableau 知識庫）