Formatting of SQL*Plus to CSV output format issues

狂风中的少年 提交于 2019-12-25 08:23:03

问题


I am trying to export the result of my SQL query to a .csv file using column formatting as below.

SET head OFF
SET feedback OFF
SET pagesize 500
SET linesize 2000;
SET colsep ,
set trimspool on
set trimout on
set trims on

column c1 heading POSTING_DATE Format date
column c2 heading COMPANY_CODE Format a6
column c3 heading PHYSICAL_ACCOUNT Format a8
column c4 heading DEBIT_AMOUNT Format 99999.99 
column c5 heading CREDIT_AMOUNT Format 99999.99 

select  POSTING_DATE c1,
  COMPANY_CODE c2,
  PHYSICAL_ACCOUNT c3,
  DEBIT_AMOUNT c4,
  EDIT_AMOUNT c5
from abc_temp;

The report is getting generated perfectly in .csv format but I am facing an issue with two columns: credit_amount and debit_amount.

When I open the .csv file in Excel and select few cells it should display the sum, count and average of all the selected cells as per the in-built Excel feature. That is not working for these two columns; only the count is displayed

It seems like Excel is treating these as strings instead of numbers. How can I fix this behaviour?


回答1:


The values are being treated as string because, by default, SQL*Plus uses tabs to space out results, and the presence of a tab makes Excel assume it is in fact a string. If it only has spaces separating the values then the value would still be treated as a number.

You can change the behaviour by adding:

set tab off

Although personally I tend to construct each row with concatenation, which eliminates any whitespace, and to format dates (and sometimes numbers) explicitly::

set head off
set pages 0

prompt Posting date,Company code,Physical account,Debit amount,Credit amount

select to_char(posting_date, 'YYYY-MM-DD')
  ||','|| company_code
  ||','|| physical_account
  ||','|| debit_amount
  ||','|| credit_amount
from abc_temp;


来源:https://stackoverflow.com/questions/40527729/formatting-of-sqlplus-to-csv-output-format-issues

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