首页 > 解决方案 > 为什么 0 是 to_char(0,'B9999') 的例外?

问题描述

是什么让数字 0 成为 Oracle to_char(number,'B9999') 掩码的例外?在这个查询中,0 根本不打印。

我怎样才能用空格填充我的所有数字,包括 0?是lpad()唯一的选择吗?

select to_char(0,'B09999') as num_fmt from dual union all
select to_char(0,'B9999') as num_fmt from dual  union all
select to_char(300,'B9999') as num_fmt from dual  union all
select to_char(-300,'B9999') as num_fmt from dual ;

标签: sqloracleto-char

解决方案


您可以使用to_char(yourVal,'999')格式模型为三位整数获取左侧填充空白(前导零为空白,零值除外)。

如果您需要更多,而不是将整数值中的九位数增加到位数:

select to_char(0,'999') as num_fmt from dual union all
select to_char('00','999')         from dual union all
select to_char('016','999')        from dual union all
select to_char(17,'999')           from dual union all
select to_char(314,'999')          from dual union all
select to_char(-314,'999')         from dual;

NUM_FMT
-------
      0
      0
     16
     17
    314
   -314

Demo


推荐阅读