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
Solved

pl/sql spool adds extra spaces to columns

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

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…
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
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.

792 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