自学内容网 自学内容网

Spark SQL----用于格式化和解析的数字Pattern

一、描述

诸如to_number和to_char之类的函数支持在字符串和Decimal类型的值之间进行转换。这些函数接受指示如何在这些类型之间进行映射的格式字符串。

二、语法

数字格式字符串支持以下语法:

  { ' [ MI | S ] [ $ ] 
      [ 0 | 9 | G | , ] [...] 
      [ . | D ] 
      [ 0 | 9 ] [...] 
      [ $ ] [ PR | MI | S ] ' }

三、Elements

每个数字格式字符串都可以包含以下元素(不区分大小写):

  • 0 or 9
    指定一个介于0和9之间的预期数字。
    格式字符串中的0或9序列与大小相同或更小的数字序列相匹配。如果0/9序列以0开头且在小数点之前,则需要精确匹配位数:解析时,仅匹配相同大小的数字序列;格式化时,结果字符串在数字序列中添加带零的左填充,以达到相同的大小。否则,0/9序列在解析时匹配大小相同或更小的任何数字序列,并在格式化时在结果字符串中用空格(如果在小数点之前)或零(如果在小数点之后)填充数字序列。请注意,如果数字序列的大小大于0/9序列,则格式化时该数字序列将变为“#”序列。
  • . or D
    指定小数点的位置。此字符只能指定一次。
    解析时,输入字符串不需要包含小数点。
  • , or G
    指定,分组(千)分隔符的位置。
    每个分组分隔符的左右两侧必须有一个0或9。解析时,输入字符串必须与与数字大小相关的分组分隔符匹配。
  • $
    指定$currency符号的位置。此字符只能指定一次。
  • 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位数长的数字,
  • 千位分隔符,
  • 小数点后最多两位数字。

5.1 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

5.2 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

5.3 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

原文地址:https://blog.csdn.net/gabriel_wang_sh/article/details/137255966

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!