

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

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

# TEXT\$1TO\$1NUMERIC\$1ALT
<a name="r_TEXT_TO_NUMERIC_ALT"></a>

TEXT\$1TO\$1NUMERIC\$1ALT 會執行 Teradata 樣式的轉換操作，將字元字串轉換為數字資料格式。

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

```
TEXT_TO_NUMERIC_ALT (expression [, 'format'] [, precision, scale])
```

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

 *表達式*   
任何評估為一或多個 CHAR 或 VARCHAR 值的運算式，例如欄名稱或常值。轉換 Null 值會傳回 Null。空白或空字串會轉換為 0。

 *format*   
字串常值，定義輸入運算式的格式。如需詳細資訊，請參閱[數值資料的 Teradata 樣式格式化字元](r_Numeric-format-teradata.md)。

 *precision*   
數字結果中的位數。預設值為 38。

 *scale*   
數值結果中小數點右邊的位數。預設值為 0。

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

TEXT\$1TO\$1NUMERIC\$1ALT 會傳回 DECIMAL 數字。

如果轉換成您指定的 *format* 詞語不成功，Amazon Redshift 就會傳回錯誤訊息。

Amazon Redshift 會將輸入 *expression* 字串轉換為數值類型，並以您在精確度選項中為該類型指定的最高 *precision*。如果數值的長度超過您為 *precision* 指定的值，Amazon Redshift 會根據下列規則四捨五入數值：
+ 如果轉換結果的長度超過您在 *format* 詞語中指定的長度，Amazon Redshift 會傳回錯誤訊息。
+ 如果將結果轉換為數值，則結果會四捨五入為最接近的值。如果小數部分剛好位於上部和下部轉換結果的中間，則結果將四捨五入到最接近的偶數。

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

下列範例會將輸入 *expression* 字串 '1.5' 轉換為數值 '2'。因為陳述式未指定 *scale*，所以 *scale* 預設為 0，而轉換結果不包含分數結果。由於 0.5 位於 1 和 2 之間，因此轉換結果將四捨五入為偶數值 2。

```
select text_to_numeric_alt('1.5');
```

```
 text_to_numeric_alt
---------------------
                   2
```

下列範例會將輸入 *expression* 字串 '2.51' 轉換為數值 3。因為陳述式未指定 *scale* 值，所以 *scale* 預設為 0，而轉換結果不包含分數結果。由於 0.51 比 2 更接近 3，因此轉換結果將四捨五入為值 3。

```
select text_to_numeric_alt('2.51');
```

```
 text_to_numeric_alt
---------------------
                   3
```

下列範例會將 *precision* 為 10 且 *scale* 為 2 的輸入 *expression* 字串 123.52501 轉換為數值 123.53。

```
select text_to_numeric_alt('123.52501', 10, 2);
```

```
 text_to_numeric_alt
---------------------
               123.53
```

以下範例會將 *format* 詞語 '999S' 的輸入 *expression* 字串 '123\$1' 轉換為數值 1230。S 字元表示帶符號的分區十進位值。如需詳細資訊，請參閱[數值資料的 Teradata 樣式格式化字元](r_Numeric-format-teradata.md)。

```
select text_to_numeric_alt('123{', '999S');
```

```
text_to_int_alt
----------
      1230
```

以下範例會將 *format* 詞語 'C9(I)' 的輸入 *expression* 字串 'USD123' 轉換為數值 124。請參閱 [數值資料的 Teradata 樣式格式化字元](r_Numeric-format-teradata.md)。

```
select text_to_numeric_alt('USD123.9', 'C9(I)');
```

```
text_to_numeric_alt
----------
       124
```

下列範例會指定資料表欄做為輸入 *expression*。

```
select text_to_numeric_alt(a), text_to_numeric_alt(b) from t_text2numeric order by 1;
```

```
           text_to_numeric_alt           |           text_to_numeric_alt
-----------------------------------------+-----------------------------------------
 -99999999999999999999999999999999999999 | -99999999999999999999999999999999999999
                                  -12300 |                                  -12300
                                     123 |                                     123
                                     123 |                                     123
  99999999999999999999999999999999999999 |  99999999999999999999999999999999999999
```

以下是此範例的表格定義和插入陳述式。

```
create table  t_text2numeric (a varchar(200), b char(200));
```

```
insert into  t_text2numeric values
('123', '123'),
('+123.456', '+123.456'),
('-' || repeat('9', 38), '-' || repeat('9', 38)),
(repeat('9', 38) || '+', repeat('9', 38) || '+'),
('-123E2', '-123E2');
```