Ugly formatting in SQL*Plus
Solution 1
Increase the linesize, e.g SET LINESIZE 32000
or use SET WRAP OFF
(but this will truncate long values)
Solution 2
SQLPlus is a simple command line tool. It's not really intended for pretty reporting. However, it does have some formatting commands, which are documented in the SQLPlus User's Guide. Find out more.
For instance you might choose to format the TITLE column to display only the first twenty characters and display the SUMMARY column in its entirety like this:
COLUMN title FORMAT a20 TRUNCATED
COLUMN summary FORMAT a4o WORD_WRAPPED
This will allow you to see your query laid out more neatly without embedding formatting commands in its projection.
Alternatively, use an IDE such as Quest's TOAD or Oracle's own SQL Developer. These tools include a query browser which automagically displays our query results in a more pleasing grid. (Other similar tools are available).
Solution 3
Make a script like below
#!/bin/ksh
FILE="/tmp/queryResult.csv"
sqlplus -s /nolog << !EOF!
connect username/password
SET PAGESIZE 50000
SET LINESIZE 250
SET NUMWIDTH 5
SET FEEDBACK OFF
set echo off
set heading on
set headsep off
set wrap off
SET COLSEP ","
column Title format a22
column Summary format a15
SPOOL $FILE
Select * from books;
SPOOL OFF
EXIT
!EOF!
Save script in a file namely sqlscript.sql set permission on file
chmode +x sqlscript.sql
run the script and pipe to less command
./sqlscript.sql | less -S
"S" option will allow you to scroll with arrow keys, if output is longer than columns set in terminal.
Alternatively you can download and open FILE="/tmp/queryResult.csv" in text editor of your choice.
Adjust the LINESIZE,NUMWIDTH, column character size (a22) as per your requirement
Solution 4
Some may not like this advice (I can think of a few DBAs who LOVE SqlPlus), but you may want to use an IDE like Toad or SQL Developer. If you're new to Oracle, sqlplus will make you feel like you just jumped back in time! IMO, spend your time learning Oracle, not SQLPlus. (oh, and read the Concepts guide while playing around in your IDE of choice)
Solution 5
Just define the column widths so that it fits the actual content of the columns
col column_name1 format a20 -- sets column to be 20 characters wide
col column_name2 format a15 -- sets column to be 15 characters wide
set line 80
select column_name1, column_name2 from books;
This should help you out.
Related videos on Youtube
Richard Knop
I'm a software engineer mostly working on backend from 2011. I have used various languages but has been mostly been writing Go code since 2014. In addition, I have been involved in lot of infra work and have experience with various public cloud platforms, Kubernetes, Terraform etc. For databases I have used lot of Postgres and MySQL but also Redis and other key value or document databases. Check some of my open source projects: https://github.com/RichardKnop/machinery https://github.com/RichardKnop/go-oauth2-server https://github.com/RichardKnop
Updated on July 09, 2022Comments
-
Richard Knop almost 2 years
It is really annoying that when I run a select command in SQL*Plus such as:
SELECT * FROM books;
The output is really badly formatted and unreadable (row cells are not in a row but separated by line breaks etc):
How can I configure it to show SELECT results in a nicer way?
EDIT:
This is my login.sql file contents:
SET ECHO OFF SET SERVEROUTPUT ON SIZE 1000000 SET PAGESIZE 999 SET LINESIZE 132
EDIT2:
Affer increasing the LINESIZE:
SET LINESIZE 32000
It now looks like this:
-
René Nyffenegger over 12 yearsYou might be interested in stackoverflow.com/questions/2228680/….
-
Trenton almost 9 yearsLots of overlap/duplication with stackoverflow.com/questions/5771573
-
-
Richard Knop about 13 years@a_horse_with_no_name Ok this helped a bit but it still looks horrific. Look at the second image I added to my question.
-
a_horse_with_no_name about 13 years@Richard: did you try
SET WRAP OFF
-
Richard Knop about 13 years@a_horse_with_no_name Yes that made it nicer but it only shows first two columns now!
-
Richard Knop about 13 yearsI remember back in school we also used SQL*Plus and there was a single command we used which made tables display nicely formatted. I can't remember what it was. And setting word wrapping for every column is not very good when you have many columns and more tables.
-
Andriy M about 13 years@Richard Knop: I think it's high time you defined the
nicer way
bit of your question more precisely. :) -
a_horse_with_no_name about 13 years@Richard Knop: as long as you don't increase the width of your terminal I don't think SQL*Plus will ever make your console window showing a horizontal scrollbar. But why don't you use a GUI tool that can makes dealing with these kind of tables a lot easier?
-
Richard Knop about 13 years@a_horse_with_no_name Well, I'm just learning and following a book where SQL*Plus is used. But don't worry, I have figured a way to make it look a little bit more readable. It's not perfect but oh well.
-
APC about 13 years@RichardKrop - I agree setting formatting for each column will get tedious. That's why it's common to use scripts for commonly run queries. I'm really not sure what that magical single omni-formatting command would have been. Perhaps it was a script which your teachers had written?
-
glenneroo over 12 yearsIf you want to make it permanent (i.e. every time you log into SQLPlus), add those lines to *glogin.sql in $ORACLE_HOME/sqlplus/admin.
-
kamran619 over 10 yearsThanks man - REALLY helped me. I was getting WAY too frustrated with the regular formatting.
-
Maitus over 5 yearsI have been pasting my format script in every session. On windows, I had to use login.sql. glogin.sql is for Unix.
-
Andrew Prock about 4 yearsPlease don't add the same answer to multiple questions. Answer the best one and flag the rest as duplicates. See Is it acceptable to add a duplicate answer to several questions?