Home » SQL & PL/SQL » SQL & PL/SQL » Inserting tabs in SQL output (Oracle Rdb V7.2-400)
Inserting tabs in SQL output [message #510874] Wed, 08 June 2011 03:07 Go to next message
fouture
Messages: 7
Registered: June 2011
Location: Leuven, Belgium
Junior Member
Hi,

I'm looking for a way to insert tabs (ASCII 009) in SQL output, to make the output file Excel-ready without manual intervention.

Here's what I need :

SQL> set output myfile.xls;
SQL> select article,TAB,count(*) from sales group by article having count(*) > 0;

Instead of TAB I can insert a dollar sign, but then I have to replace it using a file editor, which is a manual operation.

I have been looking for an OpenVMS application which can replace all dollar signs by tabs in a given file, with no luck.

I created a DCL procedure (simplified from GSR.COM which I found on the internet) that reads every record in the output file and substitutes dollar signs by tabs, but it doesn't work with tabs, only with "normal" characters...

Even if I use the OpenVMS editor to insert ASCII 009 tab characters in the SQL statement (using the GOLD 009 GOLD SPEC INS key combination), SQL apparently automatically substitutes tabs by full stop signs in the output :

select article,' ',date from werknemers limit to 2 rows;
ARTICLE DATE
TROUSERS . 12-28-1974
SHIRTS . 10-08-1973
2 rows selected

I can use SQL to create a CSV file (comma separated values) which can be opened by Excel :

"trousers",20
"shirts",30

But the user has to open this file from within Excel and make a number of choices (separator, text delimiter, header row) before (s)he gets to see the desired columnar spreadsheet. That's not automation : I need the generated SQL output file to be double-clickable so that it opens as a columnar spreadsheet immediately.

There must be a simple solution, but after two days of googling for it I still have no workable solution.

I searched this forum, and came up with zilch.

My inspiration has come to an end. So: help !?

Bart
Re: Inserting tabs in SQL output [message #510875 is a reply to message #510874] Wed, 08 June 2011 03:10 Go to previous messageGo to next message
cookiemonster
Messages: 13972
Registered: September 2008
Location: Rainy Manchester
Senior Member
SQL> select rownum||chr(9)||rownum from dual connect by level < 10;

ROWNUM||CHR(9)||ROWNUM
--------------------------------------------------------------------------------
1       1
2       2
3       3
4       4
5       5
6       6
7       7
8       8
9       9

9 rows selected.

SQL> 
Re: Inserting tabs in SQL output [message #510876 is a reply to message #510874] Wed, 08 June 2011 03:10 Go to previous messageGo to next message
muzahid
Messages: 281
Registered: September 2004
Location: Dhaka, Bangladesh
Senior Member
Use TOAD
Re: Inserting tabs in SQL output [message #510877 is a reply to message #510876] Wed, 08 June 2011 03:14 Go to previous messageGo to next message
Michel Cadot
Messages: 68770
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
And the answer does not surprise me: what is the relation between TOAD and chr(9)?

@fouture, do NOT use TOAD until you will be an expert.

Regards
Michel

[Updated on: Wed, 08 June 2011 03:19]

Report message to a moderator

Re: Inserting tabs in SQL output [message #510885 is a reply to message #510875] Wed, 08 June 2011 03:35 Go to previous messageGo to next message
fouture
Messages: 7
Registered: June 2011
Location: Leuven, Belgium
Junior Member
cookiemonster wrote on Wed, 08 June 2011 10:10
SQL> select rownum||chr(9)||rownum from dual connect by level < 10;


I'm afraid this solution, when applied to Oracle Rdb, still produces full stops in the output, not tab stops...

And I have no clue why ?!

Bart
Re: Inserting tabs in SQL output [message #510886 is a reply to message #510885] Wed, 08 June 2011 03:42 Go to previous messageGo to next message
cookiemonster
Messages: 13972
Registered: September 2008
Location: Rainy Manchester
Senior Member
You've got some weird settings somewhere converting a tab to something else - and it's not oracle that's doing it. chr(9) is tab.
Re: Inserting tabs in SQL output [message #510888 is a reply to message #510886] Wed, 08 June 2011 03:46 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
Might it be the oracle version? I really wouldn't think so, but I don't know.
Re: Inserting tabs in SQL output [message #510889 is a reply to message #510877] Wed, 08 June 2011 03:47 Go to previous messageGo to next message
muzahid
Messages: 281
Registered: September 2004
Location: Dhaka, Bangladesh
Senior Member
@michel, I told about toad because from toad he can save the output directly to excel file.
Re: Inserting tabs in SQL output [message #510891 is a reply to message #510888] Wed, 08 June 2011 03:50 Go to previous messageGo to next message
fouture
Messages: 7
Registered: June 2011
Location: Leuven, Belgium
Junior Member
Roachcoach wrote on Wed, 08 June 2011 10:46
Might it be the oracle version? I really wouldn't think so, but I don't know.


I'm using Oracle Rdb, not Oracle itself.

Oracle Rdb is using standard ANSI SQL

The only SQL settings file I know about is SYS$MANAGER:SQLINI.SQL

Omega$ type sqlini.sql

SET DATE FORMAT DATE 29;
set default date format 'VMS';
DECLARE TRANSACTION READ ONLY;

Nothing suspicious in here...
Re: Inserting tabs in SQL output [message #510893 is a reply to message #510889] Wed, 08 June 2011 03:53 Go to previous messageGo to next message
Michel Cadot
Messages: 68770
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Not excel, csv.

Regards
Michel
Re: Inserting tabs in SQL output [message #510894 is a reply to message #510889] Wed, 08 June 2011 03:53 Go to previous messageGo to next message
fouture
Messages: 7
Registered: June 2011
Location: Leuven, Belgium
Junior Member
muzahidul islam wrote on Wed, 08 June 2011 10:47
@michel, I told about toad because from toad he can save the output directly to excel file.

TOAD does not support Oracle Rdb... another dead end.
Re: Inserting tabs in SQL output [message #510895 is a reply to message #510891] Wed, 08 June 2011 03:54 Go to previous messageGo to next message
Roachcoach
Messages: 1576
Registered: May 2010
Location: UK
Senior Member
Sorry, I thought it was Oracle 7 noted in a funny manner, that's really old Smile

try

select dump('    ') from dual


See what it comes back with.
Re: Inserting tabs in SQL output [message #510897 is a reply to message #510895] Wed, 08 June 2011 03:58 Go to previous messageGo to next message
fouture
Messages: 7
Registered: June 2011
Location: Leuven, Belgium
Junior Member
Roachcoach wrote on Wed, 08 June 2011 10:54
Sorry, I thought it was Oracle 7 noted in a funny manner, that's really old Smile

try

select dump('    ') from dual


See what it comes back with.


You'd be surprised how many newspapers would be printed, or cars assembled *without* Oracle Rdb... I guess none, at least in Belgium.

But it doesn't work :

%SQL-F-RTNNOTDEF, function or procedure DUMP is not defined

I guess I'm looking for a standard ANSI SQL solution here.

[Updated on: Wed, 08 June 2011 04:00]

Report message to a moderator

Re: Inserting tabs in SQL output [message #510898 is a reply to message #510897] Wed, 08 June 2011 04:07 Go to previous messageGo to next message
Littlefoot
Messages: 21826
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
I don't know anything about "Oracle Rdb", but: can you connect to it via SQL*Plus? If so, does SET work there? If it does, you could try something like this:
SQL> set colsep ';'
SQL> spool a.csv
SQL> select * from dept;

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

SQL> spool off;
A.CSV file would have its columns separated by semi-colon (choose any other character (automatically) recognized by Excel). If you double-click that A.CSV, Excel would open it and display every (file) column in its (spreadsheet) column.
Re: Inserting tabs in SQL output [message #510899 is a reply to message #510897] Wed, 08 June 2011 04:10 Go to previous messageGo to next message
ThomasG
Messages: 3212
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
Another option might be to use a file editor that can run as a batch.

SED seems to be available for OpenVMS
Re: Inserting tabs in SQL output [message #510900 is a reply to message #510898] Wed, 08 June 2011 04:17 Go to previous messageGo to next message
fouture
Messages: 7
Registered: June 2011
Location: Leuven, Belgium
Junior Member
Littlefoot wrote on Wed, 08 June 2011 11:07
A.CSV file would have its columns separated by semi-colon (choose any other character (automatically) recognized by Excel). If you double-click that A.CSV, Excel would open it and display every (file) column in its (spreadsheet) column.


Indeed, it does Razz

As I thought : there is a simple solution !

I just have to use semicolons as separators, doublequote all text variables, and output to this SQL command :

SQL> set output myfile.csv;

Doubleclicking the file automatically launched OpenOffice, but I quickly changed that to Excel for all .csv files.

A lot of thanks !

If anyone has questions about Oracle Rdb, ModularSQL and the like on OpenVMS, I'll return the favour !

Bart
Re: Inserting tabs in SQL output [message #510915 is a reply to message #510900] Wed, 08 June 2011 06:02 Go to previous messageGo to next message
Littlefoot
Messages: 21826
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
When we're still at it, as you mentioned OpenVMS in the initial message, here's how you could replace a dollar sign using a DCL script. I know that DCL isn't what we talk about here, on OraFAQ Forum, but I simply couldn't resist.

I tried to replace it with a <Tab> (Ctrl+I in a DCL script) - it was successful, at least the output file suggested so, but - after it was FTPed to a PC, Excel ignored it and put the whole string into a single column. That's why I replaced a dollar sign with a semi-colon.

Took a while, I forgot most of it (what a shame!):

Input file (TEST_IN.TXT)
10$Little$Foot
20$Michel$Cadot
30$Cookie$Monster

Code:
$ open/read/error=end bk test_in.txt
$ open/write/error=end wt test_out.csv
$
$ rep_char = ";"
$ 
$ loop_records:
$   read/end_of_file=end bk zapis
$
$   new_zapis = ""
$   ostatak = zapis
$   l_length = f$length(zapis)
$
$   loop_in_record:
$
$     dolpos= f$locate("$", ostatak)
$     if f$length(new_zapis) .gt. l_length then goto end_1
$
$     dio = f$extract(0, dolpos, ostatak)
$     ostatak = f$extract(dolpos + 1, l_length, ostatak)
$     new_zapis = new_zapis + dio + rep_char
$   goto loop_in_record
$
$ end_1:
$   new_zapis = f$extract(0, f$length(new_zapis) - 1, new_zapis)
$   write wt new_zapis
$   goto loop_records
$
$ end:
$   close bk
$   close wt

A script reads the input file, record by record. Searches for the dollar sign (DOLPOS = $ position) and creates a new string (NEW_ZAPIS), piece by piece (within LOOP_IN_RECORD loop).

For example, "10$Little$Foot" is converted to
1st iteration: 10;
2nd iteration: 10;Little;
3rd iteration: 10;Little;Foot;
A terminating ; is removed before the new string is written into the output (TEST_OUT.CSV) file which, finally, looks like
10;Little;Foot
20;Michel;Cadot
30;Cookie;Monster

Re: Inserting tabs in SQL output [message #511014 is a reply to message #510874] Thu, 09 June 2011 01:52 Go to previous messageGo to next message
nedserd
Messages: 12
Registered: June 2011
Junior Member
For tabs you can use:

set termout off
col TAB# new_value TAB NOPRINT
select chr(9) TAB# from dual;
set colsep "&TAB"
set termout on

I have it (without the "set colsep ..") in my login.sql

Regards

Ralf



Re: Inserting tabs in SQL output [message #511046 is a reply to message #511014] Thu, 09 June 2011 03:52 Go to previous messageGo to next message
cookiemonster
Messages: 13972
Registered: September 2008
Location: Rainy Manchester
Senior Member
chr(9) doesn't work in the OPs DB. That was the main problem.
icon4.gif  Re: Inserting tabs in SQL output [message #511063 is a reply to message #510915] Thu, 09 June 2011 06:20 Go to previous message
fouture
Messages: 7
Registered: June 2011
Location: Leuven, Belgium
Junior Member
Thanks, LittleFoot:

Your DCL procedure works perfectly, and also with tabs !

I can now simply insert dollar signs in the SQL output between the columns - we only use euros over here anyway Shocked - and when finished, run this procedure to replace them with tabs.

A direct tab-delimited output would have been ideal, but this is very acceptable, thank you very much.

To the other contributors : these solutions (set termout, set colsep) do not work with Oracle Rdb, or would require the installation and mastering of additional software (only as a last resort), so thanks a lot for the suggestions but I'll do it with the DCL procedure.

Bart
Previous Topic: Session is killed but locks not released
Next Topic: Query Regarding PLSQL String Functions
Goto Forum:
  


Current Time: Fri Sep 12 15:24:14 CDT 2025