首页 > 解决方案 > 在 SQL Plus 中,当在 Oracle 11g 及更高版本中使用 lpad 时,第二列会获得额外的尾随空格

问题描述

示例代码:

spool "xy2.dat"
set heading off
set pause off
set termout off
set linesize 164
select  'start', lpad(current_date,12,'0'), current_date, 'gap', current_date, 'end' from dual
/
spool off

Oracle 10g 输出:

start 00021-DEC-20 21-DEC-20 gap 21-DEC-20 end                                                                                                                      

Oracle 11g 及以上输出:

start 00021-DEC-20                                     21-DEC-20 gap 21-DEC-20 end                                                                                  

在上面的 Oracle 11g 的 SQL Plus 中似乎有奇怪的输出,我们是否有任何根本原因导致这种不同的行为。

标签: oracle11goracle10gsqlplus

解决方案


看起来即使不显示列名(HEADING OFF),它们仍然占用空间。

由于您不需要列名,一个简单的解决方案可能是使用短列别名:

SET HEADING OFF
SELECT
    'start' AS c1,
    LPAD( CURRENT_DATE, 12, '0' ) AS c2,
    CURRENT_DATE AS c3,
    'gap' AS c4,
    CURRENT_DATE AS c5,
    'end' AS c6
FROM dual

结果:

start 00021-DEC-20 21-DEZ-20 gap 21-DEZ-20 end

这是之前的样子:

start   00021-DEC-20              21-DEZ-20    gap   21-DEZ-20      end

推荐阅读