Solved

pl/sql spool adds extra spaces to columns

Posted on 2013-10-28
6
828 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

687 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