how can I avoid white spaces in oracle query result? - oracle

I am spooling data from oracle to write it in csv file.. Its fetching blank spaces with result. Is there any way to avoid those blank spaces??

Use TRIMSPOOL
set trimspool on

SELECT RTRIM(columnname) AS columnname...

Related

trimspool ON is removing space from last column while spooling

I am spooling content of a table onto a flat file as pipe delimited with TRIMSPOOL ON Option. The default value of the last column is BLANK SPACE (' '). The trimspool is trimming this value and hence the value becomes NULL and hence validation is not succesful since this column is NOT NULL. Is there any way to solve this without altering the order of the columns.
Try SET TRIMOUT ON instead of SET TRIMSPOOL ON because it does not affect spooled output.The definition below
SET TRIMOUT ON
Determines whether SQL*Plus allows trailing blanks at the end of each
displayed line. ON removes blanks at the end of each line, which may
improve performance especially when you access SQL*Plus from a slow
communications device. TRIMOUT ON does not affect spooled output.
OR you can use NVL(col_last,' ') while loading the data

whitespace trimming in the right side of my report issue

i am using sqlplus to generate report,i will spool the output of query in sqlplus screen and making report
now i want a report which having spaces
SELECT '123'||lapd(count(1),12,'0')||rpad(' ',10',' ') from dual;
i expect the report should spool as (im showing space as dot(.) to explain you)
123000000000009..........
but i got
123000000000009
i have lot of query like this...
i am using
set trimspool on
set termout off
set linespace 300
any idea?
SET TRIMSPOOL ON removes trailing blanks at the end of each displayed or spooled line.
If that's so, and you want to keep those blanks at the end of the result, why on Earth did you use that setting? Remove it!

Avoid double quotes around column while spooling to csv from oracle

I need to export a select query output to text file using spool. The output by default adds double quotes around the column. How to Prevent it?
The query returns only one column.
Ideally i need to remove headers as well as the double quotes from the output. below is the script file i use in oracle Developer. but heading is also not removed.
set echo off
SET HEADING OFF
SET PAGESIZE 0
SET COLSEP ''
spool 'D:\public\cvs_txPCG.txt'
select /*csv*/
pcg from temptx;
spool off;
output
"PCG"
"76259737020150320000504281565213310052440093515652109.2909.290101"
"19519905620160502000504283153419040044861008644759203.3903.390101"
"49424051620160220000504284594314590009220713032964404.3804.380202"
"88761197020151025000504284594315180036700812132964401.9901.990101"
You can't get rid of the double-quotes with the /*csv*/ directive. The slightly more convenient way of doing this now is with the sqlformat command, but even with sqlformat delimited you have to have the string enclosed.
Using CSV format for a single column seems rather pointless anyway though, unless you have delimiters in your string you want to escape. If you are really exporting that one column and don't have delimiters then just leave out the /*csv*/ directive. If you have multiple columns or strings that may contain delimiters then the enclosures are useful to stop those delimiters being misinterpreted when opening in Excel or some other external tool.
If you really want CSV format without enclosures you can either build up the output manually by concatenating the column(s) with comma literals, which still allows you to spool to a file:
select pcg ||','|| some_other_col ||','|| etc from ...
Or you can run the query using Run Statement (Ctrl-Enter) and then export the results from the data grid, from the context menu that appears when you right-click on the output. When you export and choose CSV or delimited format you can choose whether to include the header and which delimiter and enclosures to use - and you're allowed to choose 'none'. If you don't want to set that on each export you can set it as a default, from Tools->Preferences:
You can't do it with the /*csv*/ directive or sqlformat csv though.
I know it is too old but got the answer with my search today, simply use
set markup csv on quote off
select 1 id, 'Venkat' name from dual;
Output will be
ID,NAME
1,Venkat
Didn't even think of any other SET commands
If the double quotes are part of the data you can use the trim() function to remove them.
select /*csv*/
trim(both '"' from pcg) as pcg from temptx;
Oracle documentation for the trim function:
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions199.htm

SQLPlus export to CSV (output format problem)

I'm facing an issue with an interface script, supposed to export the content of some table of an ORACLE database into CSV file, which is then followed by an import of those CSV into a MYSQL database.
STEP1: SQLPlus export to CSV
set headsep off
set heading off
set term off
set echo off
SET RECSEPCHAR \n
set pagesize 0
set linesize 0
trimspool on
SET FEEDBACK OFF
spool as_ex_feature.csv
select '"AS'||'"|"'||feature_group||'"|"'||feature_desc||'"|"
||feature_order||'"|"'||prod_code||'"'
from MYVIEW WHERE MYCONDITIONS;
spool off;
-> this step is generating the CSV file, but the format seems incorrect, as I can find some carriage return in the output.
Also you'll see in STEP2 that we define an "ENCLOSED BY" value how could I get that one included in the export format (doesn't seem to be the case right now).
STEP 2: MYSQL load
LOAD DATA INFILE 'mycsvfile' REPLACE INTO TABLE `mt_feature`
FIELDS TERMINATED BY '|'
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n';
This script had to be rebuilt for some technical reasons and the Mysql part had not been changed and is working fine with a proper CSV file to import.
The issue seem to be coming from that SQLPlus export, where I need to admit I don't have much knowledge on. Maybe I should use another method to get those files generated?
Please let me know if you need additional details, I feel blind...
Script running on oracle 10g, Linux, Mysql 4.x
Thanks!
SET LINESIZE 0 isn't valid, the value has to be between 1 and 32767. So I imagine it's wrapping the content at the default line length, which is 80 unless you've already got it set in a glogin script.
If you prefix any lines of code with (at least) four spaces in SO then it'll be formatted correctly, e.g.
select "AS'||'"|"'||
feature_group||'"|"'||
feature_desc||'"|"'||
feature_order||'"|"'||
prod_code||'"'
from MYVIEW
WHERE MYCONDITIONS;
Sounds like you may need to replace any embedded newline chars in the stored data....
SELECT "AS'||'"|"'||
TRANSLATE(feature_group, CHR(10), '\\n') ||'"|"'||
(etc).
And I'm not sure about setting the linesize to 0.

sqlplus remove \r \n \t from spool

Is there any sql*plus command to remove \r \n and\t from the result set that's going out to the spool file? That is, "trim" every record?
We've used set trim on in the past, but it doesn't seem to bue what we need right now. I'm trying to avoid calling oracle's translate, chr functions in the sql query.
For example,
set termout off
set spool somefile.dat
set lin 600
select data from mytable;
set spool off;
exit;
My query returns this
|DATA|
|\n \t\t\t\t\t thisistheactualdata \t\t\t\t\t\t\n|
And I'd like to keep this in my spooled file
thisistheactualdata
update
Well, we ended up doing something like this.
set tab off;
spool /home/oracle/out.dat
set linesize 20
set termout off
set trim on
select regexp_replace(l,'(\t|\n)','') from test;
spool off;
exit;
But got some bad news: We need to run this in oracle 8, and regexp_replace doesn't seem to be available. :(
Thanks in advance.
I don't think you're going to be able to do this with a SQL*Plus directive. SQL*Plus is a pretty thin client and isn't designed to touch the data from the result set itself.
If you don't want to use the built-in SQL functions to modify the output then I think you're stuck with post-processing a spooled file.
EDIT by DCookie:
Since I missed the OP's original request for a non-TRANSLATE/CHR based solution (I focused on the OP's lament that they were stuck with 8i), in fairness to dpbradley I'm going to withdraw my answer and include it as part of this one, since it was clearly considered. Here's what I wrote:
You might try the TRANSLATE function for your Oracle 8 situation:
SELECT TRANSLATE(L,'A'||CHR(10)||CHR(9)||CHR(13),'A') FROM test;
Without trying it, does
SET TAB OFF
have the desired effect?

Resources