How to format a NUMBER column in Oracle?

删除回忆录丶 提交于 2019-12-25 07:38:21

问题


I try to spool output in a nice way, so i am formatting all VARCHAR2 columns to a5. However, my problematic columns are: SLOTNUM of type NUMBER(2) and FEEADJUSTMENT of type NUMBER(5,2). My code is:

column SLOTNUM format 999;
column FEEADJUSTMENT format 999.9;
.........
column [other VARCHAR2]     format a5;
select * from allTables;

Result is:

SLOTNUM DATEV ACTUA NOTES FEEADJUSTMENT TREAT NAME                                          
------- ----- ----- ----- ------------- ----- -----                                        
     12 19-JU 19-ju Treat           2.5 12345 Flu                                              
        N-13  n-13  ment                6     Shot                                       
              00:00 succe                                                                                   
              :00   ssful                                                                                    
                    l                                                                                       

     15 20-JU 20-ju Appar           5.5 12345 Flu                                                 
        N-13  n-13  atus                6     Shot                                                         
              02:00 broke                                                                                     
              :00                                                                                              

FEEADJUSTMENT is taking more space than it needs. Why doesn't format 999.9 truncate the FEEADJUSTMENT column?


回答1:


Because it's using the length of the column header, as the documentation says:

A NUMBER column's width equals the width of the heading or the width of the FORMAT plus one space for the sign, whichever is greater.

You can alias the column with a shorter name:

select slotnum as slotn, ...

You could also treat it as a string, but you'd still need an alias really:

select to_char(slotnum, 'FM9999') as slotn, ...

You could alias is as slotnum, if you preferred, and the use format a5, but I think that would be less clear.

I'm not sure I'd describe forcing everything to a5 as 'nice', particularly as you have values lomger than that which ware forcing column wrapping. A matter of taste, I suppose...




回答2:


Format with this:

column SLOTNUM format a3;
column FEEADJUSTMENT format a5;


来源:https://stackoverflow.com/questions/17284580/how-to-format-a-number-column-in-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!