Solved

pl/sql spool adds extra spaces to columns

Posted on 2013-10-28
6
787 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 76

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 76

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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to connect SQL Server from my Oracle database? 11 93
data lookup in Oracle - need suggestions 55 112
Repeat query 13 46
having some issue on pl sql procedure 1 16
Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

813 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now