Question

I am using Oracle SQL (in SQLDeveloper, so I don't have access to SQLPLUS commands such as COLUMN) to execute a query that looks something like this:

select assigner_staff_id as staff_id, active_flag, assign_date,
  complete_date, mod_date
from work where assigner_staff_id = '2096';

The results it give me look something like this:

STAFF_ID               ACTIVE_FLAG ASSIGN_DATE               COMPLETE_DATE             MOD_DATE                  
---------------------- ----------- ------------------------- ------------------------- ------------------------- 
2096                   F           25-SEP-08                 27-SEP-08                 27-SEP-08 02.27.30.642959000 PM 
2096                   F           25-SEP-08                 25-SEP-08                 25-SEP-08 01.41.02.517321000 AM 

2 rows selected

This can very easily produce a very wide and unwieldy textual report when I'm trying to paste the results as a nicely formatted quick-n-dirty text block into an e-mail or problem report, etc. What's the best way to get rid of all tha extra white space in the output columns when I'm using just plain-vanilla Oracle SQL? So far all my web searches haven't turned up much, as all the web search results are showing me how to do it using formatting commands like COLUMN in SQLPLUS (which I don't have).

Was it helpful?

Solution

What are you using to get the results? The output you pasted looks like it's coming from SQL*PLUS. It may be that whatever tool you are using to generate the results has some method of modifying the output.

By default Oracle outputs columns based upon the width of the title or the width of the column data which ever is wider.

If you want make columns smaller you will need to either rename them or convert them to text and use substr() to make the defaults smaller.

select substr(assigner_staff_id, 8) as staff_id, 
      active_flag as Flag, 
      to_char(assign_date, 'DD/MM/YY'),
      to_char(complete_date, 'DD/MM/YY'), 
      mod_date
from work where assigner_staff_id = '2096';

OTHER TIPS

In your statement, you can specify the type of output you're looking for:

select /*csv*/ col1, col2 from table;
select /*Delimited*/ col1, col2 from table;

there are other formats available such as xml, html, text, loader, etc.

You can change the formatting of these particular options under tools > preferences > Database > Utilities > Export

Be sure to choose Run Script rather than Run Statement.

* this is for Oracle SQL Developer v3.2

What you can do with sql is limited by your tool. SQL Plus has commands to format the columns but they are not real easy to use.

One quick approach is to paste the output into excel and format it there or just attach the spreadsheet. Some tools will save the output directly as a spreadsheet.

Nice question. I really had to think about it.

One thing you could do is change your SQL so that it only returns the narrowest usable columns.

e.g. (I'm not very hot on oracle syntax, but something similar should work):

select substring( convert(varchar(4), assigner_staff_id), 1, 4 ) as id, 
       active_flag as act, -- use shorter column name

       -- etc. 

from work where assigner_staff_id = '2096';

Does that make sense?
If you were doing this on unix/linux, I would suggest running it from the command line and piping it through an awk script.

If I've miss-understood, then please update your question and I'll have another go :)

If you don't have alot of rows returned I'll often use Tom Kytes print_table function.

SQL> set serveroutput on 
SQL> execute print_table('select * from all_objects where rownum < 3');
OWNER                         : SYS
OBJECT_NAME                   : /1005bd30_LnkdConstant
SUBOBJECT_NAME                :
OBJECT_ID                     : 27574
DATA_OBJECT_ID                :
OBJECT_TYPE                   : JAVA CLASS
CREATED                       : 22-may-2008 11:41:13
LAST_DDL_TIME                 : 22-may-2008 11:41:13
TIMESTAMP                     : 2008-05-22:11:41:13
STATUS                        : VALID
TEMPORARY                     : N
GENERATED                     : N
SECONDARY                     : N
-----------------
OWNER                         : SYS
OBJECT_NAME                   : /10076b23_OraCustomDatumClosur
SUBOBJECT_NAME                :
OBJECT_ID                     : 22390
DATA_OBJECT_ID                :
OBJECT_TYPE                   : JAVA CLASS
CREATED                       : 22-may-2008 11:38:34
LAST_DDL_TIME                 : 22-may-2008 11:38:34
TIMESTAMP                     : 2008-05-22:11:38:34
STATUS                        : VALID
TEMPORARY                     : N
GENERATED                     : N
SECONDARY                     : N
-----------------

PL/SQL procedure successfully completed.

SQL> 

If its lots of rows, i'll just do the query in SQL Developer and save as xls, businessy types love excel for some reason.

Why not just use the "cast" function?

select 
(cast(assigner_staff_id as VARCHAR2(4)) AS STAFF_ID,
(cast(active_flag as VARCHAR2(1))) AS A,
(cast(assign_date as VARCHAR2(10))) AS ASSIGN_DATE,
(cast(COMPLETE_date as VARCHAR2(10))) AS COMPLETE_DATE,
(cast(mod_date as VARCHAR2(10))) AS MOD_DATE
from work where assigner_staff_id = '2096';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top