Oracle |查询输出用分号分隔

时间:2017-09-19 09:52:12

标签: sql oracle select syntax-error

我正在将.csv文件从oracle表中调整为一些值。每个值必须用分号分隔。我从中检索数据的查询是这样的:

select TEST1|| ';' ||
to_char(BATCH_DATE,'DD/MM/YYYY')|| ';' ||
to_char(START_TIME,'DD/MM/YYYY HH24:MI:SS') || ';'||
to_char(END_TIME,'DD/MM/YYYY HH24:MI:SS')  || ';'||
INPUT
from TEST_TABLE
where to_char(BATCH_DATE,'YYYYMM') = to_char(sysdate,'YYYYMM')-1
and TEST1='ENGLISH'
order by TEST1,BATCH_DATE desc;

虽然这就像魅力一样,但我还想在INPUT的末尾加一个分号。

我期待(并尝试过)这样的事情:

   select TEST1|| ';' ||
    to_char(BATCH_DATE,'DD/MM/YYYY')|| ';' ||
    to_char(START_TIME,'DD/MM/YYYY HH24:MI:SS') || ';'||
    to_char(END_TIME,'DD/MM/YYYY HH24:MI:SS')  || ';'||
    INPUT  || ';'||
    from TEST_TABLE
    where to_char(BATCH_DATE,'YYYYMM') = to_char(sysdate,'YYYYMM')-1
    and TEST1='ENGLISH'
    order by TEST1,BATCH_DATE desc;

但它不起作用,它给ORA-00936:缺少表达。

1 个答案:

答案 0 :(得分:1)

您的选择列表末尾有一个冗余的连接运算符(||):

select TEST1|| ';' ||
to_char(BATCH_DATE,'DD/MM/YYYY')|| ';' ||
to_char(START_TIME,'DD/MM/YYYY HH24:MI:SS') || ';'||
to_char(END_TIME,'DD/MM/YYYY HH24:MI:SS')  || ';'||
INPUT  || ';'
-- || removed ^
from TEST_TABLE
where to_char(BATCH_DATE,'YYYYMM') = to_char(sysdate,'YYYYMM')-1
and TEST1='ENGLISH'
order by TEST1,BATCH_DATE desc;

顺便说一句,你没有提到你是如何进行假脱机的,但是如果你正在使用SQL * Plus,你可以设置列分隔符而不必乱用连接:< / p>

-- separate columns with a semicolon  
set colsep ;

spool myfile.csv

-- Straight forward query, no concatination required:
select   TEST1,
         to_char(BATCH_DATE, 'DD/MM/YYYY')
         to_char(START_TIME, 'DD/MM/YYYY HH24:MI:SS')
         to_char(END_TIME, 'DD/MM/YYYY HH24:MI:SS')
         input
  from   TEST_TABLE
  where  to_char(BATCH_DATE, 'YYYYMM') = to_char(sysdate, 'YYYYMM') - 1 and
         TEST1='ENGLISH'
order by TEST1, BATCH_DATE desc;

spool off