Skip to Main Content
  • Questions
  • Script to extract data from database as pipe delimeter

Breadcrumb

XMAS_TREE

The AskTOM team is taking a break over the holiday season, so we're not taking questions or responding to comments, but we might still sneak in a few Office Hours sessions! Thanks for being a member of the AskTOM community.

Question and Answer

Connor McDonald

Thanks for the question, jayaraj.

Asked: April 05, 2017 - 6:37 am UTC

Last updated: April 07, 2017 - 2:29 am UTC

Version: oracle 11g

Viewed 10K+ times! This question is

You Asked

Script to extract data from Database as pipe delimiter.

I need a script to extract data from database as pipe delimiter

spool "C:\folder\table.csv"
SELECT /*csv*/ * from table where col = 100;
spool off;

sample data

1|2|3|4

and Connor said...

If you are using SQL Developer you have control over the export options

SQLDEV_EXPORT_CSV

Otherwise you'll have to hand craft it I think, ie

select col1 ||'|'|| col2 ||'|'|| col3 ...
from ...

Rating

  (2 ratings)

We're not taking comments currently, so please try again later if you want to add a comment.

Comments

with SQLCL

Rajeshwaran, April 06, 2017 - 1:19 pm UTC

SQL>
SQL> select * from scott.dept;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON

SQL> set sqlformat loader
SQL>
SQL> select * from scott.dept;
 10|"ACCOUNTING"|"NEW YORK"|
 20|"RESEARCH"|"DALLAS"|
 30|"SALES"|"CHICAGO"|
 40|"OPERATIONS"|"BOSTON"|

SQL> exit

you could even script with SQLCL like this.
C:\Users\admin>type d:\script.sql
set sqlformat loader
select * from scott.dept;
exit

C:\Users\admin>type d:\output.lst
The system cannot find the file specified.

C:\Users\admin>sql -S demo/demo@ora12c @d:\script.sql >> d:\output.lst

C:\Users\admin>type d:\output.lst
 10|"ACCOUNTING"|"NEW YORK"|
 20|"RESEARCH"|"DALLAS"|
 30|"SALES"|"CHICAGO"|
 40|"OPERATIONS"|"BOSTON"|


C:\Users\admin>

Connor McDonald
April 07, 2017 - 2:29 am UTC

Nice input.

from 12.2 client

Rajeshwaran, April 20, 2017 - 2:14 am UTC

Sorry forget to mentioned about this in the previous response.

if you have access to 12.2 client, then we can use the sqlplus "MARKUP" option to get this.

C:\Users\admin>sqlplus /nolog

SQL*Plus: Release 12.2.0.1.0 Production on Thu Apr 20 07:40:17 2017

Copyright (c) 1982, 2016, Oracle.  All rights reserved.

idle> conn demo/demo@ora11g
Connected.
demo@ORA11G>
demo@ORA11G> select * from scott.dept ;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON

demo@ORA11G> set markup csv on delimiter | quote on
demo@ORA11G> select * from scott.dept ;

"DEPTNO"|"DNAME"|"LOC"
10|"ACCOUNTING"|"NEW YORK"
20|"RESEARCH"|"DALLAS"
30|"SALES"|"CHICAGO"
40|"OPERATIONS"|"BOSTON"

demo@ORA11G>