简体   繁体   English

Oracle编号问题

[英]Oracle Number Issue

Good Day, 美好的一天,

I have an issue I could use your help with. 我有一个问题可以帮助您。

A customer would like invoice amounts displayed as follows: 客户希望发票金额显示如下:

Example Invoice Amounts 发票金额示例

-405.12 to be shown as 000000040512

&

-400.00 to be shown as 000000040000

The following query works fine for the 405.12 amount, but for the 400.00 amount it drops the two zeros on the right side 以下查询适用于405.12的金额,但对于400.00的金额,它会在右侧删除两个零

LPAD(REPLACE((invoiceamt*-1),'.',''),12,0) 

How may I solve this issue? 我该如何解决这个问题?

Thank You Aaron 谢谢亚伦

I suggest 我建议

 to_Char(abs(invoiceamt) * 100, '000000000000')

where 哪里

    abs  - absolute value - get rid of sign (-)
  * 100  - removing decimal point
to_Char  - final formatting (12 mandatory digits)

忘记替换,只需将发票乘以-100,然后将LPAD乘以所需的长度即可。

Another question has reminded me of the existence of the V format model element : 另一个问题让我想起V格式模型元素的存在:

Returns a value multiplied by 10^ n (and if necessary, round it up), where n is the number of 9's after the V . 返回一个乘以10 ^ n的值(如有必要,将其四舍五入),其中nV之后的9的数目。

But you can use zeros instead of 9s to keep leading zeros, and add FM to remove the leading space (which is there for a - symbol for negative values - which you won't have). 但是,您可以使用零而不是9来保持前导零,并添加FM来删除前导空格(在其中-负值的符号-您将没有)。 So you can also do: 因此,您也可以执行以下操作:

with t (n) as (
  select -405.12 from dual
  union all select -400 from dual
)
select n, to_char(abs(n), 'FM0000000000V00') as result
from t;

         N RESULT       
---------- -------------
   -405.12 000000040512 
      -400 000000040000 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM