How to preserve leading zeros when converting to a decimal in oracle

寵の児 提交于 2020-01-15 09:22:47

问题


I am running the below query in oracle.

WITH
ta AS (
     SELECT account_coid
        ,txn_id
        ,cbdev.cbzdt(effective_date) AS effective_date
        ,cbdev.cbchr(utl_raw.substr(txn_data, 113, 20)) AS CESG_amt
     FROM bs_transaction
     WHERE sub_type = 127469880)
SELECT
cast(ta.CESG_amt as DECIMAL (20,2)) AS cesg_amt
from ta 
inner join ....

Here, i m getting the result (cesg_amt) as -156.57. But i need the result as -0000000000156.57.
I need the leading zeros with - retained (leading 0's and also the two digits after the decimal).

I have tried as to_char(ta.CESG_amt, '0000000000000.00') AS cesg_amt in the query but of no use.

Can you please help me what needs to be done in the DECIMAL field to get the result as below.


回答1:


You may use such a formatting :

select to_char(-156.57,'fm0000000000000D00','NLS_NUMERIC_CHARACTERS = ''.,''')
       as Result
  from dual;

RESULT
-----------------
-0000000000156.57



回答2:


  select to_char(-156.57,'000000000.00')
  from dual;


来源:https://stackoverflow.com/questions/53411625/how-to-preserve-leading-zeros-when-converting-to-a-decimal-in-oracle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!