用於格式化和解析的數字模式
說明
函數(例如 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 來補齊,以達到相同的大小。否則,0/9 序列會在解析時對應到大小相同或較小的任何數字序列,並在格式化時在結果字串中對數字序列加上空白(如果位於小數點之前)或 0(如果位於小數點之後)。請注意,如果大小大於 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