SQL将长整数缩短为带两位小数的4或5个字符的数字
我正在尝试编写SQL代码,将此SQL将长整数缩短为带两位小数的4或5个字符的数字,sql,oracle,oracle-sqldeveloper,Sql,Oracle,Oracle Sqldeveloper,我正在尝试编写SQL代码,将此0000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000?
以下是我到目前为止的情况:
to_number(to_char(substr(trim(to_char(nvl(pre_tax,0),'00000000000000000000000000000000')),1,8),'9999999.99'))/100 as pre_tax,
trim(to_char(substr(to_char(nvl(pre_tax,0),'00000000000000000000000000000000'),9,8),'99999999'))/100000 as pre_tax_pct,
to_number(to_char(substr(trim(to_char(nvl(roth,0),'00000000000000000000000000000000')),1,8),'9999999.99'))/100 as roth_amt,
trim(to_char(substr(to_char(nvl(roth,0),'00000000000000000000000000000000'),9,8),'99999999'))/100000 as roth_pct
很难看到您的代码当前在做什么,但这将满足您的需求(最后一个“final_outp”专栏,其他专栏只展示如何构建它)
怎么样
SQL> WITH vals AS ( /* Test input */
2 SELECT '00004000000000000000000000000000' AS input_value FROM dual
3 UNION
4 SELECT '00025000000000000000000000000000' FROM dual
5 )
6 select to_char(input_value / 10E25, '999G990D00') result
7 from vals;
RESULT
-----------
40,00
250,00
SQL>
从您的查询尝试来看,似乎您有两个固定格式的32个字符串,分别称为税前
和roth
;您需要从每个块中提取出前8个字符和第二个8个字符的块,将它们转换为数字,并将它们显示为格式化文本。第一个区块应该代表小数点前的六位有效数字和小数点后的两位有效数字;而第二个数字在小数点前有三位,在小数点后有五位。也许
因此,您可以使用:
-- CTE for sample data, including a second row with more digits
with your_table (pre_tax, roth) as (
select '00004000000000000000000000000000', '00025000000000000000000000000000' from dual
union all
select '12345678901234567890000000000000', null from dual
)
-- actual query
select to_char(nvl(to_number(substr(pre_tax, 1, 8)), 0) / 100, '999990D99') as pre_tax_amt,
to_char(nvl(to_number(substr(pre_tax, 9, 8)), 0) / 100000, '990D99999') as pre_tax_pct,
to_char(nvl(to_number(substr(roth, 1, 8)), 0) / 100, '9999990D99') as roth_amt,
to_char(nvl(to_number(substr(roth, 9, 8)), 0) / 100000, '990D99999') as roth_pct
from your_table;
PRE_TAX_AM PRE_TAX_PC ROTH_AMT ROTH_PCT
---------- ---------- ----------- ----------
40.00 0.00000 250.00 0.00000
123456.78 901.23456 0.00 0.00000
整个字符串应该代表一个数字吗?或者-考虑到你目前的子串方式,至少2个;可能是3个,每个8位数?包括第二/第三部分的非零元素,以及您希望如何呈现这些元素,可能会有所帮助。
-- CTE for sample data, including a second row with more digits
with your_table (pre_tax, roth) as (
select '00004000000000000000000000000000', '00025000000000000000000000000000' from dual
union all
select '12345678901234567890000000000000', null from dual
)
-- actual query
select to_char(nvl(to_number(substr(pre_tax, 1, 8)), 0) / 100, '999990D99') as pre_tax_amt,
to_char(nvl(to_number(substr(pre_tax, 9, 8)), 0) / 100000, '990D99999') as pre_tax_pct,
to_char(nvl(to_number(substr(roth, 1, 8)), 0) / 100, '9999990D99') as roth_amt,
to_char(nvl(to_number(substr(roth, 9, 8)), 0) / 100000, '990D99999') as roth_pct
from your_table;
PRE_TAX_AM PRE_TAX_PC ROTH_AMT ROTH_PCT
---------- ---------- ----------- ----------
40.00 0.00000 250.00 0.00000
123456.78 901.23456 0.00 0.00000