问题
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