Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to remove trailing white spaces in columns while using Spool

4068702Aug 21 2019 — edited Aug 21 2019

SET PAGESIZE 1000

SET COLSEP ,

SET WRAP OFF

SET HEADSEP OFF

SET UNDERLINE OFF

SET TRIMSPOOL ON

SET TRIMOUT ON

SET LINESIZE 2000

SET TERMOUT OFF

SET FEEDBACK OFF

SPOOL C:\Oracle\documents\emp.csv

SELECT * FROM EMP;

SPOOL OFF

Output is as following

EmployeeNumber,Employeename,EmployeeDepartment,EmployeeDesignation,EmployeeSalary

                    7657,             Andrew,                 Accounting,                                   ,               50000,

Comments

Mustafa KALAYCI

what is your client version? if it is 12c and above you can simply use:

set markup csv on

select .....

Paulzip

Use

set markup csv on

if 12c or above

or

You'll have to do something like...

select EmployeeNumber||','||Employeename||','||','||EmployeeDepartment||','||EmployeeDesignation||','||EmployeeSalary

from emp

and remember to use to_char on fields if it's a number or a date.

4068702

Hello Paul,

I have already tried Set markup csv , it is not working, I am using oracle 11g. I cannot use every column separately as I have to use it for a large database and am looking for a dynamic query as i will use it on certain views.

4068702

Hi Mustafa,

It is not working, already tried. I cannot change the query I have to use just select * from table name. I cannot use every other column names separately as it is a reusable code.

Karthick2003

Try sqlcl tool.

https://www.oracle.com/database/technologies/appdev/sqlcl.html

In this tool all you need is this

select /*csv*/ * from emp;

This tool has lots of interesting features.

4068702

Hi Karthick,

Thanks for your help, but we use Toad in our organisation.

Mustafa KALAYCI

your example commands are sqlplus commands! so those are application specific. Toad, I assume, allows you to run sqlplus commands. I simply suggest you install an oracle 12c client. you don't need to have a 12c database. just use new client ans your sqlplus will be able to run set markup csv command.

[Deleted User]

8132c5da-3111-4c9a-865a-bcfd06ff02ac wrote:

we use Toad in our organisation.

A quick search using Google gave me this: https://forums.toadworld.com/t/export-the-results-of-a-query-to-csv/29959 ...

That said: What exactly are you trying to achieve?

 I cannot use every column separately as I have to use it for a large database and am looking for a dynamic query as i will use it on certain views.

Why is the size of the database an issue? Why would you need something "dynamic" (I hate that word...) for views?

 I cannot change the query I have to use just select * from table name. I cannot use every other column names separately as it is a reusable code.

Reusable code? What code? If you need help with code you need to show us that code.

4068702

Hi Stefan,

What exactly are you trying to achieve?

I want to create a csv file from DB which will be transferred in between app and DB servers.

Why is the size of the database an issue? Why would you need something "dynamic" (I hate that word...) for views?

Using all column names is not possible for as not every view might contain all.

Reusable code? What code?

One single procedure which for all views.  This particular set of select statement I have to use inside the procedure.

code is confidential.

Thanks for your help.

AndrewSayer

8132c5da-3111-4c9a-865a-bcfd06ff02ac wrote:

Hi Stefan,

What exactly are you trying to achieve?

I want to create a csv file from DB which will be transferred in between app and DB servers.


..

Why?

Your app should be using SQL to get data. It should not go back to the stone ages and parse a CSV file...

What's the real business problem you're trying to solve here?

Gaz in Oz

As others have alluded to , Oracle sqlplus 12.2 and above has the "set markup csv on"" command.

Sqlplus 12.2.0.1.0 New Features

instantclient and sqlplus is available from here:

https://www.oracle.com/database/technologies/instant-client/downloads.html

If you are using tOAd then go look in a tOAd forum:

https://forums.toadworld.com/t/export-the-results-of-a-query-to-csv/29959

If you want to stick with your current script then change the query to actually select columns concatenated with the comma.

For example:

...

select col1||','||col2||','||col3...

from   table

where ...

[Deleted User]

8132c5da-3111-4c9a-865a-bcfd06ff02ac wrote:

Using all column names is not possible for as not every view might contain all.

Sorry, I don't understand what you mean. If really necessary you could use the data dictionaries to get every column name from every table and every view, without typing a single column name yourself (well, data dictionary columns excepted of course)

8132c5da-3111-4c9a-865a-bcfd06ff02ac wrote:

One single procedure which for all views. This particular set of select statement I have to use inside the procedure.

So do you mean that you want a single procedure that exports all your views to csv?

8132c5da-3111-4c9a-865a-bcfd06ff02ac wrote:

code is confidential.

What is so confidential about a procedure that exports table contents to a csv? If I google "oracle export to csv" I get hundreds of results. Surely there must be one that you can use....

It would be beneficial to you if you clearly explain what the business problem is that you try to resolve with an export to csv. And I do not mean that the business says that they need an export to csv, I'm talking about the problem above that. Why do they need that?

1 - 12

Post Details

Added on Aug 21 2019
12 comments
3,141 views