Home » SQL & PL/SQL » SQL & PL/SQL » how to escape comma while exporting data from table into csv file. (10g)
how to escape comma while exporting data from table into csv file. [message #550455] Mon, 09 April 2012 11:48 Go to next message
kiran111
Messages: 49
Registered: October 2011
Member
hi friends,

how we can escape comma while exporting data from table into csv file.

CREATE TABLE emp
(
  EMPNO     NUMBER(4)                           NOT NULL,
  ENAME     VARCHAR2(10 BYTE),
  JOB       VARCHAR2(9 BYTE),
  MGR       NUMBER(4),
  HIREDATE  DATE,
  address   varchar2(100),
  SAL       NUMBER(7,2),
  COMM      NUMBER(7,2),
  DEPTNO    NUMBER(2)
);


Insert into emp
   (EMPNO, ENAME, JOB, MGR, ADDRESS, HIREDATE, SAL, DEPTNO)
 Values
   (7369, 'SMITH', 'CLERK', 7902, 'PEI,CANADA', TO_DATE('12/17/1980 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 800, 20);
Insert into emp
   (EMPNO, ENAME, JOB, MGR, ADDRESS, HIREDATE, SAL, DEPTNO)
 Values
   (7499, 'ALLEN', 'SALESMAN', 7698, 'Bangalore,INDIA', TO_DATE('02/20/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 1600, 30);
Insert into emp
   (EMPNO, ENAME, JOB, MGR, ADDRESS, HIREDATE, SAL, DEPTNO)
 Values
   (7521, 'WARD', 'SALESMAN', 7698, '80,hyderabad,india', TO_DATE('02/22/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 1250, 30);
Insert into emp
   (EMPNO, ENAME, JOB, MGR, ADDRESS, HIREDATE, SAL, DEPTNO)
 Values
   (7566, 'JONES', 'MANAGER', 7839, '#20,PEI,canada', TO_DATE('04/02/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2975, 20);
Insert into emp
   (EMPNO, ENAME, JOB, MGR, ADDRESS, HIREDATE, SAL, DEPTNO)
 Values
   (7782, 'CLARK', 'MANAGER', 7839, '#10-20,US', TO_DATE('06/09/1981 00:00:00', 'MM/DD/YYYY HH24:MI:SS'), 2450, 10);


i have to export data from emp table which has address column and address column contain comma,
when i am running below script, the comma part in address field comes in next tab in csv file, is there any way we can avoid shifting to next tab and can have complete address in one tab.
set echo off
set verify off
set termout on
set heading off
set pages 50000
set feedback off
set newpage none
set linesize 160
spool e:/file_csv.csv

SELECT 'EMPNO,ENAME,JOB,ADDRESS,MGR,HIREDATE,DEPTNO' from dual
union all
SELECT empno||','||ename||','||job||','||address||','||mgr||','||hiredate||','||DEPTNO
FROM emp
WHERE deptno in (10,20);

spool off;
exit;
Re: how to escape comma while exporting data from table into csv file. [message #550458 is a reply to message #550455] Mon, 09 April 2012 11:56 Go to previous messageGo to next message
Michel Cadot
Messages: 68767
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Enclose the address between ".
SELECT empno||','||ename||','||job||',"'||address||'",'||mgr||','||hiredate||','||DEPTNO

Regards
Michel

Re: how to escape comma while exporting data from table into csv file. [message #550460 is a reply to message #550458] Mon, 09 April 2012 12:07 Go to previous messageGo to next message
Littlefoot
Messages: 21826
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Or, REPLACE a comma in address column with something else (semi-colon, perhaps?).
Re: how to escape comma while exporting data from table into csv file. [message #550462 is a reply to message #550460] Mon, 09 April 2012 12:27 Go to previous messageGo to next message
LKBrwn_DBA
Messages: 487
Registered: July 2003
Location: WPB, FL
Senior Member
OR....

Use a different field separator, like perhaps "pipe" (|)?

[Updated on: Mon, 09 April 2012 12:37] by Moderator

Report message to a moderator

Re: how to escape comma while exporting data from table into csv file. [message #550498 is a reply to message #550462] Mon, 09 April 2012 21:16 Go to previous messageGo to next message
kiran111
Messages: 49
Registered: October 2011
Member
Hi Michel,
many thanks, it works Smile

Hi LKBrwn_DBA,
can you please let me know how to use the different field seperator,
i tried, but now all data are coming in only first tab instead of seperate tab in csv file.
Re: how to escape comma while exporting data from table into csv file. [message #550499 is a reply to message #550498] Mon, 09 April 2012 21:21 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
SQL> SET COLSEP |
Re: how to escape comma while exporting data from table into csv file. [message #550506 is a reply to message #550499] Mon, 09 April 2012 23:59 Go to previous messageGo to next message
kiran111
Messages: 49
Registered: October 2011
Member
Hi BlackSwan,

I did the same, but still all data coming in single tab.
below is the code,please let me know where I am doing mistake.

set echo off
set verify off
set termout on
set heading off
set pages 50000
set feedback off
set newpage none
set linesize 160
set colsep '|'

spool e:/file_csv.csv

SELECT 'EMPNO|ENAME|JOB|ADDRESS|MGR|HIREDATE|DEPTNO' from dual
union all
SELECT empno||'|'||ename||'|'||job||'|'||address||'|'||mgr||'|'||hiredate||'|'||DEPTNO
FROM emp
WHERE deptno in (10,20);

spool off;
exit;
Re: how to escape comma while exporting data from table into csv file. [message #550507 is a reply to message #550506] Tue, 10 April 2012 00:05 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
SELECT empno,ename,job,address,mgr,hiredate,DEPTNO
FROM emp
WHERE deptno in (10,20);
Re: how to escape comma while exporting data from table into csv file. [message #550527 is a reply to message #550498] Tue, 10 April 2012 02:07 Go to previous messageGo to next message
flyboy
Messages: 1903
Registered: November 2006
Senior Member
kiran111 wrote on Tue, 10 April 2012 04:16
i tried, but now all data are coming in only first tab instead of seperate tab in csv file.

Did you not forget to tell your favourite spreadsheet application, that the separator character is now pipe '|' and not comma ','?

As Excel takes it directly from Windows Control Panel -> Regional and Language Options settings (at least its online help says so - you really should get acquainted with it; by the way, I found semicolon ';' in that setting), it is probably not wise to change it as it may affect behaviour of other programs/activities. Especially when enclosing strings between double quotes (") (as Michel advised) produces correct result.
Re: how to escape comma while exporting data from table into csv file. [message #550608 is a reply to message #550527] Tue, 10 April 2012 21:11 Go to previous message
kiran111
Messages: 49
Registered: October 2011
Member
thanks all for your time and explanation
Previous Topic: SQL Query
Next Topic: Assigning numbers to the list!!
Goto Forum:
  


Current Time: Sat Aug 23 23:58:06 CDT 2025