views:

13503

answers:

4

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).

+2  A: 

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';
Thomas Jones-Low
As mentioned, I'm using Oracle SQLDeveloper (free tool--I'm cheap!). It may use SQL*PLUS under the covers, but it doesn't let me use the COLUMN command or most other SQL*PLUS commands.Using substr(col, width) is what I was after though--that helps! Great!
Ogre Psalm33
A: 

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 :)

AJ
I tried pasting a piece of your answer into SQLDeveloper, but Oracle seems to choke on the convert. I think Oracle's convert works differently. The use of substring (substr) is helpful, though.
Ogre Psalm33
A: 

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.

Ok, that's a decent option. I just checked, and SQL Developer does have an option to export results as a CVS or XLS file.
Ogre Psalm33
A: 

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.

Matthew Watson