格式化和解析的数字模式
描述
诸如 to_number
和 to_char
之类的函数支持在字符串和 Decimal 类型的值之间进行转换。 这些函数接受格式字符串,指示如何在这些类型之间进行映射。
语法
数字格式字符串支持以下语法
{ ' [ MI | S ] [ $ ]
[ 0 | 9 | G | , ] [...]
[ . | D ]
[ 0 | 9 ] [...]
[ $ ] [ PR | MI | S ] ' }
元素
每个数字格式字符串可以包含以下元素(不区分大小写)
-
0
或9
指定一个介于
0
和9
之间的预期数字。格式字符串中的 0 或 9 序列与相同或更小大小的数字序列匹配。 如果 0/9 序列以 0 开头并且位于小数点之前,则需要精确匹配数字位数:在解析时,它仅匹配相同大小的数字序列; 在格式化时,结果字符串会用零向左填充数字序列,使其达到相同的大小。 否则,0/9 序列在解析时匹配任何具有相同或更小大小的数字序列,并在格式化时用空格(如果在小数点之前)或零(如果在小数点之后)填充结果字符串中的数字序列。 请注意,如果大小大于 0/9 序列,则数字序列在格式化时将变为“#”序列。
-
.
或D
指定小数点的位置。 此字符只能指定一次。
解析时,输入字符串不需要包含小数点。
-
,
或G
指定
,
分组(千位)分隔符的位置。每个分组分隔符的左侧和右侧都必须有一个
0
或9
。 解析时,输入字符串必须匹配与数字大小相关的分组分隔符。 -
$
指定
$
货币符号的位置。 此字符只能指定一次。 -
S
指定可选的“+”或“-”符号的位置。 此字符只能指定一次。
-
MI
指定可选的“ - ”符号(没有“ + ”)。 此字符只能指定一次。
格式化时,它为正值打印一个空格。
-
PR
将负数输入值映射到相应字符串中的包装尖括号 (
<1>
)。正数输入值不会收到包装尖括号。
函数类型和错误处理
to_number
函数接受输入字符串和格式字符串参数。 它要求输入字符串与提供的格式匹配,否则会引发错误。 然后,该函数返回相应的 Decimal 值。try_to_number
函数接受输入字符串和格式字符串参数。 它的工作方式与to_number
函数相同,不同之处在于,如果输入字符串与给定的数字格式不匹配,它将返回 NULL 而不是引发错误。to_char
函数接受输入十进制数和格式字符串参数。 然后,该函数返回相应的字符串值。- 如果给定的格式字符串无效,则所有函数都将失败。
示例
以下示例使用 to_number
, try_to_number
, 和 to_char
SQL 函数.
请注意,这些示例中使用的大多数格式字符串都期望
- 开头有一个可选符号,
- 后跟一个美元符号,
- 后跟一个介于 3 到 6 位数字之间的数字,
- 千位分隔符,
- 小数点后最多两位数字。
to_number
函数
-- The negative number with currency symbol maps to characters in the format string.
> SELECT to_number('-$12,345.67', 'S$999,099.99');
-12345.67
-- The '$' sign is not optional.
> SELECT to_number('5', '$9');
Error: the input string does not match the given number format
-- The plus sign is optional, and so are fractional digits.
> SELECT to_number('$345', 'S$999,099.99');
345.00
-- The format requires at least three digits.
> SELECT to_number('$45', 'S$999,099.99');
Error: the input string does not match the given number format
-- The format requires at least three digits.
> SELECT to_number('$045', 'S$999,099.99');
45.00
-- MI indicates an optional minus sign at the beginning or end of the input string.
> SELECT to_number('1234-', '999999MI');
-1234
-- PR indicates optional wrapping angel brakets.
> SELECT to_number('9', '999PR')
9
try_to_number
函数
-- The '$' sign is not optional.
> SELECT try_to_number('5', '$9');
NULL
-- The format requires at least three digits.
> SELECT try_to_number('$45', 'S$999,099.99');
NULL
to_char
函数
> SELECT to_char(decimal(454), '999');
"454"
-- '99' can format digit sequence with a smaller size.
> SELECT to_char(decimal(1), '99.9');
" 1.0"
-- '000' left-pads 0 for digit sequence with a smaller size.
> SELECT to_char(decimal(45.1), '000.00');
"045.10"
> SELECT to_char(decimal(12454), '99,999');
"12,454"
-- digit sequence with a larger size leads to '#' sequence.
> SELECT to_char(decimal(78.12), '$9.99');
"$#.##"
-- 'S' can be at the end.
> SELECT to_char(decimal(-12454.8), '99,999.9S');
"12,454.8-"
> SELECT to_char(decimal(12454.8), 'L99,999.9');
Error: cannot resolve 'to_char(Decimal(12454.8), 'L99,999.9')' due to data type mismatch:
Unexpected character 'L' found in the format string 'L99,999.9'; the structure of the format
string must match: [MI|S] [$] [0|9|G|,]* [.|D] [0|9]* [$] [PR|MI|S]; line 1 pos 25