如何使用 Oracle 将此输入转换为数字

How can I convert this input in a number with Oracle

我无法从声明为 varchar 的字段中找到正确的十进制数转换。

甲骨文 12c

源信息说他们设置了这种格式: 'S9999999999999V,99'

我使用了 TO_NUMBER() 函数,但找不到正确的转换和参数来正确读取和转换源代码。

只有 TO_NUMBER(字段) 发送和无效数字错误。

数据接收示例

  1. +0000000000160,00
  2. -9999999999999,99

我一直收到无效号码错误。

源信息格式中的V看起来不对; from the documentation:

Element Example Description
V 999V99 Returns a value multiplied by 10n (and if necessary, round it up), where n is the number of 9's after the V.

如果模型的其余部分是正确的,并且看起来与两个示例匹配,那么您可能想要:

to_number(source, 'S9999999999999D99', 'NLS_NUMERIC_CHARACTERS=,.')

fmt部分跟你说的一样,只是把V去掉,用D标示小数点; nls 部分表示小数点分隔符是逗号。

-- CTE for sample data
with your_table (source) as (
  select '+0000000000160,00' from dual
  union all
  select '-9999999999999,99' from dual
)
-- query against sample data
select source,
  to_number(source, 'S9999999999999D99', 'NLS_NUMERIC_CHARACTERS=,.') as result
from your_table

给予

SOURCE            RESULT
----------------- -----------------
+0000000000160,00               160
-9999999999999,99 -9999999999999.99

客户端将选择是否使用逗号或句点小数点分隔符显示转换后的值,通常通过 NLS 设置。您可以更改该会话设置,或将数字显式转换回特定格式的字符串。

db<>fiddle 显示默认和修改后的会话输出。