如何在我的oracle SQL查询结果中添加小数位

问题描述 投票:0回答:1

我有以下疑问

select oc.card_number,
       case opt.oper_type
       when 'OPTP0000' then '05-PURCHASE' 
       when 'OPTP0020' then '06-CREDIT VOUCHER' 
       else 'other' end as Transaction_Type,
       opt.oper_date as "TRANSACTION_DATE", 
       op.account_number, 
       case vf.sttl_currency
       when '840' then 'USD'
       end as "SOURCE_AMOUNT_CURRENCY", 
       case opt.oper_type
       when 'OPTP0000' then  -vf.sttl_amount
       else  vf.sttl_amount
       end as "SOURCE_AMOUNT",
       op.auth_code, 
       opt.mcc, 
       opt.terminal_number as "TERMINAL_ID", 
       vf.pos_terminal_cap, 
       vf.pos_entry_mode,
       opt.part_key as "CAPTURE_DATE", 
       opt.oper_amount as "TRANSACTION_AMOUNT",
       opt.oper_currency as "TRANSACTION_CURRENCY",
       opt.merchant_name,
       COALESCE(cc.visa_country_code,opt.merchant_country) merchant_country,
       opt.clearing_sequence_num, 
       opt.clearing_sequence_count, 
       opt.oper_type
FROM   opr_operation opt 
       INNER JOIN opr_participant op
       ON op.oper_id = opt.id
       INNER JOIN opr_card oc
       ON oc.oper_id = opt.id
       INNER JOIN vis_fin_message vf
       ON     opt.id = vf.id
       INNER JOIN  com_country cc
       ON cc.code= opt.merchant_country 
          AND op.card_id = vf.card_id
WHERE  opt.clearing_sequence_num > 1
AND    opt.part_key >= DATE '2024-03-01'
AND    opt.msg_type IN ('MSGTPAMC', 'MSGTPACC')
AND    op.participant_type = 'PRTYISS'

给出以下输出:

"CARD_NUMBER"   "TRANSACTION_TYPE"  "TRANSACTION_DATE"  "ACCOUNT_NUMBER"    "SOURCE_AMOUNT_CURRENCY"    "SOURCE_AMOUNT" "AUTH_CODE" "MCC"   "TERMINAL_ID"   "POS_TERMINAL_CAP"  "POS_ENTRY_MODE"    "CAPTURE_DATE"  "TRANSACTION_AMOUNT"    "TRANSACTION_CURRENCY"  "MERCHANT_NAME" "MERCHANT_COUNTRY"  "CLEARING_SEQUENCE_NUM" "CLEARING_SEQUENCE_COUNT"   "OPER_TYPE"
"1234567891234567"  "05-PURCHASE"   25-MAR-24   "004000485011"  "USD"   -2020   "718515"    "5734"  "TERMID01"  "9" "01"    27-MAR-24   2020    "060"   "ACQUIRER NAME            " "BM"    2   2   "OPTP0000"

但是,是否可以让 F 列 - 'SOURCE_AMOUNT' 返回具有 2 位小数的值?

所以在上面的例子中,-2020应该显示为-20.20

我确实尝试按如下方式执行 to_char 但它不正确并且不起作用

 case opt.oper_type
       when 'OPTP0000' then to_char (coalesce (-vf.sttl_amount,0)999999.99)
       else  vf.sttl_amount
       end as "SOURCE_AMOUNT",

如有任何帮助,我们将非常感激。

sql oracle oracle-sqldeveloper
1个回答
0
投票

TO_CHAR
与适当的格式模型应用于整个
CASE
表达式,例如

   TO_CHAR (
      CASE opt.oper_type
         WHEN 'OPTP0000' THEN COALESCE (-vf.sttl_amount, 0)
         ELSE vf.sttl_amount
      END,
      '999G990D00') AS SOURCE_AMOUNT

这会将千位分隔符和小数点字符应用于该值。检查文档以了解其他可能性。


虽然与你的问题无关:尽量不要在Oracle中使用双引号;在绝大多数情况下,它们是不需要的。由于您将它们用作列/表达式别名,并且它们全部都是大写字母,单词之间带有下划线字符,所以

source_amount
SOURCE_AMOUNT
"SOURCE_AMOUNT"
和类似的之间没有区别。

© www.soinside.com 2019 - 2024. All rights reserved.