Solved

pl/sql spool adds extra spaces to columns

Posted on 2013-10-28
6
808 Views
Last Modified: 2015-01-05
i have sql file - myfile.sql  which i created to export data from table in a '|' separated text file

spool off;
set linesize 500
set pagesize 50000
SET HEADING OFF
set COLSEP '~'
SET newpage none
SET verify off
SET term off
SET trims ON
SET trimspool ON
set serveroutput ON
set echo off

spool mydata.txt

select ltrim(rtrim(mycol1)) , ltrim(rtrim(mycol2)) , '|' from mytable1;
spool off;
exit;


Now if I execute myfile.sql, it automatically adds lots of spaces to both the columns
for e.g if value of mycol1 is 'asdf'  and mycol2 is 'abcd'
it spools it  as
asdf                      |abcd                      |

pl help

i need trimmed values for all the columns in the spooled text file mydata.txt
0
Comment
Question by:at999
  • 3
  • 2
6 Comments
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39607124
Use string concatination.

In 11g there is listagg.

or the old manual version:
select ltrim(rtrim(mycol1)) || '|' ||  ltrim(rtrim(mycol2))  || '|' from mytable1;

If this can be greater than 4000 characters, I suggest the XML approach:
http://www.experts-exchange.com/Database/Oracle/Q_24914739.html#a25864822


Also: PL/SQL is Oracle's Procedural language.  What you have is just called SQL.
0
 

Author Comment

by:at999
ID: 39607135
yes i already did  ltrim(rtrim( mycol1) and ltrim(rtrim(mycol2)

but still it is  putting extra space.... pl help
0
 
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 350 total points
ID: 39607145
>>yes i already did  ltrim(rtrim( mycol1) and ltrim(rtrim(mycol2)

I see that but you aren't using string concatenation.  Look at what I posted...

|| is a string concatenator in Oracle:

select 'Hello' || ' ' || 'World' from dual;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:at999
ID: 39607151
oh kkk thanks a lot let me try that
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 39607156
also, if all you are concerned with is spaces, no need for both an ltrim and rtrim.  There is a single trim function:

select trim(mycol1) || '|' ||  trim(mycol2)  || '|' from mytable1;
0
 
LVL 35

Accepted Solution

by:
Mark Geerlings earned 150 total points
ID: 39608737
The formatting commands you use ("set..." ) are all SQL*Plus commands, not PL\SQL commands.  PL\SQL is Oracle's programming language for stored procedures.  SQL*Plus is Oracle's application or programmer's interface for running either SQL or PL\SQL commands, and the commands you are using here are simple SQL commands.

I've never needed to use "trim" in this setting, but that assumes the columns are VARCHAR2 columns.  If the columns you are selecting from are actually defined as CHAR columns (which are supported, but not as common in most Oracle databases) then you will need the "trim" operator to remove the trailing spaces.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle Listener Not Starting 11 65
Sybase and replication server 13 58
subtr returning incorrect value 8 67
running myfile.SQL from command line SQLPLUS 12c does not exit. 7 36
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question