用于格式化和解析的数字模式
描述
诸如 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
函数接受一个输入 Decimal 值和一个格式字符串参数。然后,该函数返回相应的字符串值。- 如果给定的格式字符串无效,所有函数都将失败。
示例
以下示例使用 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 brackets.
> 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