0

I am using sqlplus in shell script to connect to DB and spool .csv file.

code is:-

sqlplus -s user/pass@db <<EOF> /dev/null 2>&1
SET COLSEP ','
SET FEEDBACK OFF
SET TRIMSPOOL ON
SET UNDERLINE OFF
SET LINESIZE 10000
SET PAGESIZE 10000

spool customer.csv 

select * from customer where cust_name='john';
spool off
EOF

output:- enter image description here

i get my header trim for few Colum's like 1st, 2nd 4th and 7th.

expected output:- enter image description here

1 Answer 1

2

You'd need to tell SQL*Plus how wide you want the columns to be. Assuming I've counted the length of each header correctly

column sys_creation_date format a17
column sys_update_date   format a15
column dl_service        format a10
column eff_date_time     format a13

You may be happier switching, though, to SQLcl since that makes generating CSV files easier. Just

select /*csv*/ * from emp

or

set sqlformat csv
select * from emp
Sign up to request clarification or add additional context in comments.

5 Comments

do we have any option to set format to all columns?
here...setting format will help only if columns are less , what if we have 50 -70 columns, then this is not a good idea.
@Roger - If you're using SQL*Plus, and you want to make sure that a column header prints out completely, you'd need to format the column. Yes, that's potentially 70 lines of formatting if your query has 70 columns. Which is why SQLcl is generally the better option unless you're wedded to older tools for some reason.
@JustinCave - sqlcli is not needed to use the /*csv*/ directive. sqlplus (at least on 19c) will handle it as well.
@EdStevens - Interesting. I knew SQL Developer and SQLcl supported that. Wasn't aware it had been added to SQL*Plus.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.