Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format Oracle SQL text-only select output

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

like image 346
Ogre Psalm33 Avatar asked Sep 27 '08 18:09

Ogre Psalm33


People also ask

How do I format in Oracle?

In SQL statements, you can use a format model as an argument of the TO_CHAR and TO_DATE functions to specify: The format for Oracle to use to return a value from the database. The format for a value you have specified for Oracle to store in the database.

How do I change the format of a SQL result?

I presume you are trying to change the result view in SQL Server Management Studio. If this the case what you need is 'Result to Grid' option. You can also use Ctrl + D to change the view to grid and 'Ctrl + T' to change it back to text.


2 Answers

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

like image 131
Jim Clouse Avatar answered Oct 13 '22 01:10

Jim Clouse


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';
like image 23
Thomas Jones-Low Avatar answered Oct 13 '22 00:10

Thomas Jones-Low