SQL加:如何防止数字自动四舍五入
SQL Plus: How to prevent the auto rounding of the number
我需要使用 SQL Plus 将 Oracle 数据库值提取到 csv 以比较小数点为 10 位的数值。
但是数字会自动四舍五入到小数点后 8 位。有什么方法可以禁用 SQLPlus 中的数字舍入。详情请见下方。
这是我的 SQL 设置命令。
SET UNDERLINE OFF
SET PAGESIZE 50000
SET NEWPAGE NONE
SET WRAP OFF
SET COLSEP , -- SEPARATE COLUMNS WITH A COMMA
SET FEEDBACK OFF
SET TRIMS ON
SET TRIMSPOOL ON
SET DEFINE ON
SET termout ON
SET verify ON
SET linesize 600
谁能帮我解决这个问题。
数值没有任何固有格式,因此您的客户正在决定如何显示它们。我不确定哪个客户端正在生成 'DB' 值,但 SQL*Plus 只是 using its defaults:
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.
所以你看到了:
with your_table (x, y) as (
select -0.0224231886, -0.021470109 from dual
union all select -0.037164512, -0.0238026527 from dual
union all select 0.021786217, 0.044550243 from dual
union all select 0.0772262609, 0.0724136521 from dual
union all select 0.968632046, 0.0866250777 from dual
)
select x, y from your_table;
X, Y
-.02242319,-.02147011
-.03716451,-.02380265
.021786217,.044550243
.077226261,.072413652
.968632046,.086625078
您可以使用 `set numf[ormat]' 更改会话的默认值,使用小数点前后有足够数字的格式模型来表示您可能存储的任何数字,例如:
set numformat 990.99999999999
然后相同的查询得到
X, Y
-0.0224231886, -0.0214701090
-0.0371645120, -0.0238026527
0.0217862170, 0.0445502430
0.0772262609, 0.0724136521
0.9686320460, 0.0866250777
或者您可以使用 set numw[idth]
:
隐式修改它
set numf ""
set numwidth 20
X, Y
-.0224231886, -.021470109
-.037164512, -.0238026527
.021786217, .044550243
.0772262609, .0724136521
.968632046, .0866250777
它为您提供所需的精度,但不显示前导零。如果这无关紧要,那么这种方法就更简单了。
也许您寻求列格式选项来强制列格式
COLUMN column_name FORMAT 999999.0000000000
我需要使用 SQL Plus 将 Oracle 数据库值提取到 csv 以比较小数点为 10 位的数值。
但是数字会自动四舍五入到小数点后 8 位。有什么方法可以禁用 SQLPlus 中的数字舍入。详情请见下方。
这是我的 SQL 设置命令。
SET UNDERLINE OFF
SET PAGESIZE 50000
SET NEWPAGE NONE
SET WRAP OFF
SET COLSEP , -- SEPARATE COLUMNS WITH A COMMA
SET FEEDBACK OFF
SET TRIMS ON
SET TRIMSPOOL ON
SET DEFINE ON
SET termout ON
SET verify ON
SET linesize 600
谁能帮我解决这个问题。
数值没有任何固有格式,因此您的客户正在决定如何显示它们。我不确定哪个客户端正在生成 'DB' 值,但 SQL*Plus 只是 using its defaults:
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 theSET
command (normally 10). If a number is larger than the value ofSET 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.
所以你看到了:
with your_table (x, y) as (
select -0.0224231886, -0.021470109 from dual
union all select -0.037164512, -0.0238026527 from dual
union all select 0.021786217, 0.044550243 from dual
union all select 0.0772262609, 0.0724136521 from dual
union all select 0.968632046, 0.0866250777 from dual
)
select x, y from your_table;
X, Y
-.02242319,-.02147011
-.03716451,-.02380265
.021786217,.044550243
.077226261,.072413652
.968632046,.086625078
您可以使用 `set numf[ormat]' 更改会话的默认值,使用小数点前后有足够数字的格式模型来表示您可能存储的任何数字,例如:
set numformat 990.99999999999
然后相同的查询得到
X, Y
-0.0224231886, -0.0214701090
-0.0371645120, -0.0238026527
0.0217862170, 0.0445502430
0.0772262609, 0.0724136521
0.9686320460, 0.0866250777
或者您可以使用 set numw[idth]
:
set numf ""
set numwidth 20
X, Y
-.0224231886, -.021470109
-.037164512, -.0238026527
.021786217, .044550243
.0772262609, .0724136521
.968632046, .0866250777
它为您提供所需的精度,但不显示前导零。如果这无关紧要,那么这种方法就更简单了。
也许您寻求列格式选项来强制列格式
COLUMN column_name FORMAT 999999.0000000000