平面文件数据导出 - 如果数字字段为空,则固定列长度

时间:2016-09-15 09:15:09

标签: oracle oracle11g data-extraction utl-file

我有一个临时表,其中包含我希望导出到具有固定位置的平面文件的应用程序数据。我最喜欢的是数字列account_numbers中的一些值为null。在导出期间,空值行不会替换为空格,而是设置为null。我怀疑我必须在填充之前将列转换为char,但它似乎无法解决问题。 我错过了什么?

RPAD(To_char(i.Account_Number),11,'')

创建表格插入数据:

Declare
  pFile Utl_File.file_type;
  fLine Varchar2(1024);

Begin

  pFile := UTL_FILE.fopen('USER_TEMP', 'APPLIC_EXPORT.txt', 'w');

  For i in (Select V.ACCOUNT_NUMBER ,V.CREATED_DATE,TRIM(V.DECISION) DECISION,TRIM(V.NAME) NAME From V_APPLICATION_INSTANCE V) 
  Loop

    fLine := RPAD(i.Account_Number,11,' ') || ' ' || RPAD(i.Created_Date,30,' ') || ' ' ||
             RPAD(i.Decision,25,' ') || ' ' || RPAD(i.Name,50,' ');
    UTL_FILE.put_line(pFile, convert(fLine, 'WE8ISO8859P1', 'UTF8'));

  End Loop;

  UTL_FILE.fclose(pFile);
End;

导出到平面文件:

 ===================================================== == == == =========== == == == == == == == == == ===== 
  1679669315  09-SEP-16 10.43.51.932000 AM   APPROVED              TAF2214                                   
   31-AUG-16 12.41.53.331000 PM   APPROVED                 TAF2234                                   
  1528296849  02-SEP-16 12.00.32.008000 PM   APPROVED              TAF1234                                   
   31-AUG-16 05.05.30.433000 PM   APPROVED                 TAF1234                                   
 ===================================================== == == == =========== == == == == == == == == == =====

输出:

 ===================================================== == == == =========== == == == == == == == == == ===== 
  1679669315  09-SEP-16 10.43.51.932000 AM   APPROVED              TAF2214                                   
              31-AUG-16 12.41.53.331000 PM   APPROVED              TAF2234                                   
  1528296849  02-SEP-16 12.00.32.008000 PM   APPROVED              TAF1234                                   
              31-AUG-16 05.05.30.433000 PM   APPROVED              TAF1234                                   
 ===================================================== == == == =========== == == == == == == == == == =====

预期结果:

{{1}}

由于

1 个答案:

答案 0 :(得分:0)

很抱歉回答我自己的问题,但我终于找到了解决方案。 不得不用空格替换null

rpad(nvl(TO_CHAR(i.account_number),''),11,'')