Avoid double quotes around column while spooling to csv from oracle - 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

Related

sql*plus is truncating the columns names according to the values in that columns

I have two broad questions:
Q1. Are executing the lines of code in Oracle SQL Developer and executing the same code in sqlplus command prompt same things?
(reason I ask this that I heard that not all the sqlplus commands are executable in SQL Developer. If answer to above question is yes then please then few useful links will help).
Q2. I am spooling the results of a sql query to a .csv file, but the thing is that columns names are truncated according the maximum length of the values in that column.
My code is:
set colsep ","
spool C:\Oracle\sample.csv
select * from acct_clas_rule;
spool off;
Output of above code is (middle column is having null values)
ABCD_,ABCD_,ABC
-----,-----,---
AB , ,WSD
ABCD , ,WSD
ABCD , ,WSD
SG , ,WSD
KD , ,WSD
WD , ,LKJ
KLHGF, ,LKO
WSDFG, ,LOK
WSDF , ,LKO
WS , ,GH
In above output, columns names have been truncated. I want full names of the columns to be displayed. Can anyone help?
I have seen the question in this link, but I didn't understand how to apply the answers provided there as there was no particular example cited. I am new to these things so I couldn't understand.
Original names of my columns (from left to right in above table) are :
ABCD_DFGT_SDF, ABCD_EDF_GH, ABCD_DFRE
PS -
1. I am using Oracle SQL developer to run sqlplus commands. I think because of which few of my commands are not working (like set underline, set linesize etc.).Please let me know if this is the case. I actually want remove those underlines beneath the columns names.
2. Also let me know that whether you answer is applicable to Oracle SQL Developer or sqlplus.
Thank You
There are a couple of things you can do, in addition to #JChomel's approach - that will work in either SQL Develoepr or SQL*Plus, while these suggestions are specific to SQL Developer.
Let's start with a dummy query based on a CTE to get something like your situation:
set colsep ","
with acct_clas_rule (abdc_1, abcd_2, abcd_3) as (
select cast('AB' as varchar2(5)), cast(null as varchar2(5)), cast('WSD' as varchar2(4)) from dual
union all select 'ABCD', null, 'WSD' from dual
-- ...
union all select 'WS', null, 'GH' from dual
)
select * from acct_clas_rule;
When run as a script in SQL Developer (from the document+arrow icon, or F5) the output is:
ABDC_,ABCD_,ABCD
-----,-----,----
AB , ,WSD
ABCD , ,WSD
WS , ,GH
If you change the query to include the SQL Developer-specific formatting hint /*csv*/ then you get the output you want:
select /*csv*/ * from acct_clas_rule;
"ABDC_1","ABCD_2","ABCD_3"
"AB","","WSD"
"ABCD","","WSD"
"WS","","GH"
except that the strings are all enclosed in double-quotes, which might not be what you really want. (It depends what you're doing with the spooled file and whether any of your string values contain commas, which would confuse Excel for instance).
With more recent versions of SQL Developer you can get exactly the same result without modifying the query using the sqlformat option:
set sqlformat csv
select * from acct_clas_rule;
but again you get the double-quotes. You could change the double-quotes to different enclosure characters, but that probably doesn't help.
A different approach is to use the built-in export tools instead of spool. If you run the query as a statement (green 'play button' icon, or control-enter) then rather than appearing in the Script Output panel, a new Query Result panel will open next to that, showing the results as a grid.
If you right-click on the results you'll get a contextual menu, and choosing Export... from that will give you a wizard to export the results in a format of your choice, including CSV:
You can leave the left and right enclosures as double-quotes to get the same results as the options above, except that null values use the word 'null' instead of an empty string:
"ABDC_1","ABCD_2","ABCD_3"
"AB",null,"WSD"
"ABCD",null,"WSD"
"WS",null,"GH
or you can change them, or remove them by choosing 'none', which gives you:
ABDC_1,ABCD_2,ABCD_3
AB,null,WSD
ABCD,null,WSD
WS,null,G
#thatjeffsmith has commented on how to change the null-replacement text for the /*csv*/ approach. For export it looks like having the word 'null' might have been a bug in 17.2.0; in 17.3.1 that does not appear, and instead you see:
ABDC_1,ABCD_2,ABCD_3
AB,,WSD
ABCD,,WSD
WS,,GH
or enclosed empty strings ("") if you leave the enclosures set.
Q1: TI'm not an expert of SQL Developer. There might be a mode like "command line" or something where you could get similar result, but unsure.
Q2: You have to set the right option to sqlplus: here is a trick I know of (it will also remove the --- --- --- that will cause other issue):
SET HEADING OFF`, to avoid column name to be printed
Union all the column names at the beginning of your script:
set colsep ","
spool C:\Oracle\sample.csv
select 'ABCD_DFGT_SDF', 'ABCD_EDF_GH', 'ABCD_DFRE' from dual
UNION ALL
select * from acct_clas_rule;
spool off;
use the sql plus set command -----> set underline off

Export to CSV in Oracle SQL Developer not quoting values

I'm trying to export the results of a query in Oracle SQL Developer. One of my columns is a user-defined type which looks like this when it gets exported:
SDE.ST_GEOMETRY(1,1,2702721.09480406,249404.580511138,2702721.09480406,249404.580511138,NULL,NULL,NULL,NULL,0,0,2272,'oracle.sql.BLOB#615bd3ac')
The problem is that SQL Developer doesn't quote values of this column, even though I've specified left and right enclosures of ", and the commas inside the parentheses break my CSV file.
Is there any way to force SQL Developer to enclose all values on export?
As seen from your problem add extra pipes in beginning and end of the column as like:
select ||'|'||column1||'|'|| from table1;

While exporting data from Oracle PLSQL to Excel, format issue is coming

I am trying to export data from Oracle PLSQL to Excel.
The data type of one of the column is VARCHAR2.
The Column conatins value like 00798019859217.
But after exporting, the value in excel is something like this 7.9802E+11.
PLease let me know how to resolve this and the reason for this format issue.
Thanks in advance.
Do the following:
Select the column with single quotes, say
SELECT ("'" || COLUMN_NAME) AS COLUMN_NAME, OTHER_COLUMNS FROM MY_TABLE
Output will be like:
'ABC0157976
'00798019859217
Export the output to an excel.In excel "A" column values will be
'ABC0157976
00798019859217 (Single quote will not be visible for number only values)
Select the entire "A" row and clear all single quotes with replace all option. You will get final excel as.
ABC0157976
00798019859217
Since it is a text field and non-numeric characters are also expected to be present, the step#3 is required. If it is going to be only numeric characters, then step #3 can be ignored.
This is happening due to excel where the number auto casted. As #HamidP said try exporting as csv and check with opening in notepad or text, and check whether the number displaying fully or not. If so then you can open the same in Excel with small options change such
Right click the cell and click format option and make it cell format as text and then save the excel.

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.

Showing only actual column data in SQL*Plus

I'm spooling out delimited text files from SQL*Plus, but every column is printed as the full size per its definition, rather than the data actually in that row.
For instance, a column defined as 10 characters, with a row value of "test", is printing out as "test " instead of "test". I can confirm this by selecting the column along with the value of its LENGTH function. It prints "test |4".
It kind of defeats the purpose of a delimiter if it forces me into fixed-width. Is there a SET option that will fix this, or some other way to make it print only the actual column data.
I don't want to add TRIM to every column, because if a value is actually stored with spaces I want to be able to keep them.
Thanks
I have seen many SQL*plus script, that create text files like this:
select A || ';' || B || ';' || C || ';' || D
from T
where ...
It's a strong indication to me that you can't just switch to variable length output with a SET command.
Instead of ';' you can of course use any other delimiter. And it's up to your query to properly escape any characters that could be confused with a delimiter or a line feed.
Generally, I'd forget SQL Plus as a method for getting CSV out of Oracle.
Tom Kyte has written a nice little Pro-C unloader
Personally I've written a utility which does similar but in perl

Resources