【问题标题】:Why is 0 an exception for to_char(0,'B9999')?为什么 0 是 to_char(0,'B9999') 的例外?
【发布时间】:2019-09-08 00:50:23
【问题描述】:

是什么让数字 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 ;

【问题讨论】:

  • B - Returns blanks for the integer part of a fixed-point number when the integer part is zero (regardless of zeros in the format model) 来自doc

标签: sql oracle to-char


【解决方案1】:

您可以使用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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 2018-05-01
    相关资源
    最近更新 更多