简体   繁体   English

SPOOLING SQL SELECT 语句到 CSV 文件 Oracle 12C - 如果不在最右侧,则日期列显示为空白

[英]SPOOLING SQL SELECT statement to CSV File Oracle 12C - Date column comes out blank if not at far right hand side

Im working on systemising some reporting which is done each day, I have created sql select statements which return the required data and I'm now looking to have these run overnight and saved to a location.我正在对每天完成的一些报告进行系统化,我创建了返回所需数据的 sql select 语句,我现在希望让这些语句在一夜之间运行并保存到某个位置。

We export and save files in a single string as CSV with a ' |我们将文件以单个字符串的形式导出并保存为 CSV 并带有 ' | ' delimiter, using this approach if i have a date inbetween other columns then this date column comes out blank. ' 分隔符,如果我在其他列之间有一个日期,则使用这种方法,则此日期列显示为空白。 If i have it formatted as a string using to_char(date, 'YYYYMMDD') then it will work correctly however to work with it further in Excel or similar the user will need to convert this date.如果我使用to_char(date, 'YYYYMMDD')将其格式化为字符串to_char(date, 'YYYYMMDD')那么它将正常工作,但是要在 Excel 或类似工具中进一步使用它,用户将需要转换此日期。 If i have this date field on the last column ie the further to the right then the date exports as expected without converting to string.如果我在最后一列有这个日期字段,即更靠右的日期,那么日期会按预期导出而不转换为字符串。

Can anyone advise why this is the case and if there is a way to have date information with other columns either side?任何人都可以建议为什么会出现这种情况,以及是否有办法将日期信息与任何一侧的其他列一起使用?

Code we are currently using which produces columns in the right order but date needs to be converted我们目前使用的代码以正确的顺序生成列,但需要转换日期

WHENEVER OSERROR EXIT FAILURE ROLLBACK;
SET SERVEROUTPUT ON SIZE 100000
WHENEVER SQLERROR EXIT SQL.SQLCODE;

SET TERMOUT OFF
SET HEADING OFF
SET TRIMSPOOL ON
SET FEEDBACK OFF
SET ECHO OFF
SET PAGESIZE 40000
SET LINESIZE 2000
SET NEWPAGE NONE

COLUMN filename    NEW_VAL filename
 SELECT 'FILEoutput'||TO_CHAR (SYSDATE, 'YYYYMMDD')||'.txt' filename
   FROM dual;
SPOOL /data/out/&filename
    select 'SKU'||'|'||'DATE'||'|'||'QTY'||'|'||'SEG' from dual;
        select dat.SKU ||'|'|| TO_CHAR (TRUNC (dat.date), 'YYYYMMDD') ||'|'|| dat.qty ||'|'|| dat.Seg 
        from scpomgr.dat;
SPOOL OFF;
EXIT;

If I remove the to_char then I get a blank column:如果我删除 to_char 然后我得到一个空白列:

COLUMN filename    NEW_VAL filename
 SELECT 'FILEoutput'||TO_CHAR (SYSDATE, 'YYYYMMDD')||'.txt' filename
   FROM dual;
SPOOL /data/out/&filename
    select 'SKU'||'|'||'DATE'||'|'||'QTY'||'|'||'SEG' from dual;
        select dat.SKU ||'|'|| TRUNC (dat.date) ||'|'|| dat.qty ||'|'|| dat.Seg 
        from scpomgr.dat;
SPOOL OFF;
EXIT;

But if I put the date at the right it comes out correctly:但是如果我把日期放在右边,它会正确显示:

COLUMN filename    NEW_VAL filename
 SELECT 'FILEoutput'||TO_CHAR (SYSDATE, 'YYYYMMDD')||'.txt' filename
   FROM dual;
SPOOL /data/out/&filename
    select 'SKU'||'|'||'QTY'||'|'||'SEG'||'|'||'DATE' from dual;
        select dat.SKU ||'|'|| dat.QTY  ||'|'|| dat.SEG ||'|'|| TRUNC (dat.date)
        from scpomgr.dat;
SPOOL OFF;
EXIT;

These issues are only found if I run the export job and look at the CSV, if I just run these select statements everything looks OK.只有当我运行导出作业并查看 CSV 时才会发现这些问题,如果我只运行这些选择语句,一切看起来都不错。

We are using Oracle 12C我们正在使用 Oracle 12C

I've tried googling and searching for an answer but have been unsuccessful in finding the same scenario - Would appreciate any help or direction on this,我试过谷歌搜索并寻找答案,但没有成功找到相同的场景 - 希望对此有任何帮助或指导,

Using the code使用代码

TO_CHAR (TRUNC (dat.date), 'DD/MM/YYYY') 

then presents the data correctly and allows the column to be placed between other columns instead of at the end which solves the original question.然后正确呈现数据并允许将列放置在其他列之间而不是放在解决原始问题的末尾。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM