假脱机截断十进制数据

spool truncates decimal data

我正在做一个非常简单的查询(对一列求和):

select 
SUM (txn.amt_val) AS monto
from siebel.s_loy_txn txn 

查询结果是791412524.16,但是在做spool的时候,得到的结果被截断了。

所以:791412524

完整的查询是这样的-->

SET serveroutput ON SIZE 1000000;
SET pages 0;
SET linesize 15000;
SET heading OFF;
SET verify OFF;
SET echo OFF;
SET feedback OFF;
SET term OFF;
SET trims ON;

Spool 'C:\Users\Report.txt' --spoolname from dual;

Select 
SUM (txn.amt_val) AS monto
from siebel.s_loy_txn txn;


SPOOL off

也许明确地将数字转换为足够长度的字符?

select cast(txn.amt_val as char(20)) as monto from siebel.s_loy_txn txn;

SQL*Plus中默认的numwidth是10。结合默认的numformat意味着它显示十进制值的十位有效数字;在这种情况下,它隐式地将小数点四舍五入,而不是截断。如果您的结果是 91412524.16,您将看到 91412524.2。

这是描述in the documentation:

SQL*Plus normally displays numbers with as many digits as are required for accuracy, up to a standard display width determined by the value of the NUMWIDTH variable of the SET command (normally 10). If a number is larger than the value of SET NUMWIDTH, SQL*Plus rounds the number up or down to the maximum number of characters allowed if possible, or displays hashes if the number is too large.

...虽然不是特别清楚。

您可以将 numwidth 变大,在本例中至少为 12;或设置 numformat 以显式包含小数(可选 fm 以抑制前导空格和尾随零);或者使用 to_char() 和类似的掩码;或者按照 CZ Zhu 的建议,你可以施放效果相同但不太明显的方法。

测试所有这些选项:

-- default numwidth and numformat
select 791412524.16 from dual;

set numwidth 11
select 791412524.16 from dual;

set numwidth 12
select 791412524.16 from dual;

set numwidth 10
set numformat fm999999999999999.99999
select 791412524.16 from dual;

set numformat ""
select to_char(791412524.16, 'fm999999999999999.99999') from dual;

select cast(791412524.16 as varchar2(20)) from dual;

其中得到:

   791412524
 791412524.2
791412524.16
791412524.16
791412524.16
791412524.16

这个问题被标记为 SQL 开发人员,但是由于您使用的页面大小错误,您似乎正在使用 SQL*Plus。同样的事情也适用于 SQL Developer,除了它的默认 numformat 不太友好,给你 7.9E+08。更改 numwidth 没有帮助,但其他版本在 SQL Developer 中也获得了全部价值。