

 从补丁 198 开始，Amazon Redshift 将不再支持创建新的 Python UDF。现有的 Python UDF 将继续正常运行至 2026 年 6 月 30 日。有关更多信息，请参阅[博客文章](https://www.amazonaws.cn/blogs/big-data/amazon-redshift-python-user-defined-functions-will-reach-end-of-support-after-june-30-2026/)。

# TO\$1NUMBER
<a name="r_TO_NUMBER"></a>

TO\$1NUMBER 将字符串转换为数字（小数）值。

**注意**  
我们建议您在格式字符串中使用 `FM` 来禁止填补空格和零。有关有效格式的列表，请参阅[数字格式字符串](r_Numeric_formating.md)。

## 语法
<a name="r_TO_NUMBER-synopsis"></a>

```
to_number(string, format)
```

## 参数
<a name="r_TO_NUMBER-arguments"></a>

 *string*   
要转换的字符串。格式必须是文本值。

 *format*   
第二个参数是指示应如何分析字符串以创建数字值的格式字符串。例如，格式 `'FM99D999'` 指定要转换的字符串包含五位数，其中小数点在第三位。例如，`to_number('12.345','FM99D999')` 将 `12.345` 作为数字值返回。有关有效格式的列表，请参阅[数字格式字符串](r_Numeric_formating.md)。

## 返回类型
<a name="r_TO_NUMBER-return-type"></a>

TO\$1NUMBER 返回 DECIMAL 数。

如果转换为*格式* 失败，则返回错误。

## 示例
<a name="r_TO_NUMBER-examples"></a>

以下示例将字符串 `12,454.8-` 转换为数字：

```
select to_number('12,454.8-', 'FM99G999D9S');

to_number
-----------
-12454.8
```

以下示例将字符串 `$ 12,454.88` 转换为数字：

```
select to_number('$ 12,454.88', 'FML99G999D99');

to_number
-----------
12454.88
```

以下示例将字符串 `$ 2,012,454.88` 转换为数字：

```
select to_number('$ 2,012,454.88', 'FML9,999,999.99');

to_number
-----------
2012454.88
```