Solved

pl/sql spool adds extra spaces to columns

Posted on 2013-10-28
6
759 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 34

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.

Join & Write a Comment

Suggested Solutions

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.  …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

759 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