首页 > 解决方案 > SQL to_char 舍入 '9999.99'

问题描述

您好,我想知道是否有人可以向我解释什么 to_char(number, '999.99') 以及当 9 变为 0 时它如何影响数字的输出。

例如,如果我有数字 123.75 或 123.44,如果它是 '999.90' '990.00' '999.99' 会发生什么

只是想了解 9 和 0 在上下文中的含义

标签: sqloracle

解决方案


它在任何Oracle 数据库版本的TO_CHAR函数文档中都有解释,在Format models部分下。

  • 0:返回前导/尾随零
  • 9:
    • 返回具有指定位数的值,如果为正,则带有前导空格,如果为负,则返回前导减号。
    • 前导零为空白,但零值除外,它为定点数的整数部分返回零。

例如:

SQL> with test (col) as
  2    (select 123.70 from dual union all
  3     select  10.07 from dual
  4    )
  5  select col,
  6         to_char(col, '999.90') val_1,
  7         to_char(col, '990.00') val_2,
  8         to_char(col, '999.99') val_3
  9  from test;

       COL VAL_1   VAL_2   VAL_3
---------- ------- ------- -------
     123,7  123.70  123.70  123.70
     10,07   10.07   10.07   10.07

SQL>

前导零:

SQL> with test (col) as
  2    (select 123.70 from dual union all
  3     select  10.07 from dual
  4    )
  5  select col,
  6         to_char(col, '0999.90') val_1,
  7         to_char(col, '0990.00') val_2,
  8         to_char(col, '0999.99') val_3
  9  from test;

       COL VAL_1    VAL_2    VAL_3
---------- -------- -------- --------
     123,7  0123.70  0123.70  0123.70
     10,07  0010.07  0010.07  0010.07
            --       --       --         --> these are leading zeros
SQL>

推荐阅读