SQL*Plus User's Guide and Reference

Contents Index Home Previous Next

COLUMN

Purpose

Specifies display attributes for a given column, such as

Also lists the current display attributes for a single column or all columns.

Syntax

COL[UMN] [{column|expr} [option ...]]

where option represents one of the following clauses:

ALI[AS] alias
CLE[AR]
FOLD_A[FTER]
FOLD_B[EFORE]
FOR[MAT] format
HEA[DING] text
JUS[TIFY] {L[EFT]|C[ENTER]|C[ENTRE]|R[IGHT]}
LIKE {expr|alias}
NEWL[INE]
NEW_V[ALUE] variable
NOPRI[NT]|PRI[NT]
NUL[L] text
OLD_V[ALUE] variable
ON|OFF
WRA[PPED]|WOR[D_WRAPPED]|TRU[NCATED]

Terms and Clauses

Enter COLUMN followed by column or expr and no other clauses to list the current display attributes for only the specified column or expression. Enter COLUMN with no clauses to list all current column display attributes.

Refer to the following list for a description of each term or clause:

{column|expr} Identifies the data item (typically, the name of a column) in a SQL SELECT command to which the column command refers. If you use an expression in a COLUMN command, you must enter expr exactly as it appears in the SELECT command. If the expression in the SELECT command is a+b, for example, you cannot use b+a or (a+b) in a COLUMN command to refer to the expression in the SELECT command.
If you select columns with the same name from different tables, a COLUMN command for that column name will apply to both columns. That is, a COLUMN command for the column ENAME applies to all columns named ENAME that you reference in this session. COLUMN ignores table name prefixes in SELECT commands. Also, spaces are ignored unless the name is placed in double quotes.
To format the columns differently, assign a unique alias to each column within the SELECT command itself (do not use the ALIAS clause of the COLUMN command) and enter a COLUMN command for each column's alias.
ALI[AS] alias Assigns a specified alias to a column, which can be used to refer to the column in BREAK, COMPUTE, and other COLUMN commands.
Note: A SQL*Plus alias is different from a SQL alias. See the Oracle7 Server SQL Language Reference Manual for further information on the SQL alias.

CLE[AR] Resets the display attributes for the column to default values.
To reset the attributes for all columns, use the CLEAR COLUMNS command.
FOLD_A[FTER] Inserts a carriage return after the column heading and after each row in the column. SQL*Plus does not insert an extra carriage return after the last column in the SELECT list.
FOLD_B[EFORE] Inserts a carriage return before the column heading and before each row of the column. SQL*Plus does not insert an extra carriage return before the first column in the SELECT list.
FOR[MAT] format Specifies the display format of the column. The format specification must be a text constant such as A10 or $9,999--not a variable.
Character Columns The default width of CHAR and VARCHAR2 (VARCHAR) columns is the width of the column in the database. SQL*Plus formats CHAR and VARCHAR2 (VARCHAR) data left-justified. If a value does not fit within the column width, SQL*Plus wraps or truncates the character string depending on the setting of SET WRAP. The width cannot exceed 32,767 or the value set with SET MAXDATA. (VARCHAR2 requires Oracle7.)
A LONG column's width defaults to the value of SET LONGCHUNKSIZE or SET LONG, whichever one is smaller.
A Trusted Oracle column of datatype MLSLABEL or RAW MLSLABEL defaults to the width defined for the column in the database or the length of the column's heading, whichever is longer. The default display width for a Trusted Oracle column of datatype ROWLABEL is 15.
To change the width of a CHAR, VARCHAR2 (VARCHAR), LONG, or Trusted Oracle column to n, use FORMAT An. (A stands for alphanumeric.) If you specify a width shorter than the column heading, SQL*Plus truncates the heading. If you specify a width for a LONG column, SQL*Plus uses the LONGCHUNKSIZE or the specified width, whichever is smaller, as the column width.
DATE Columns For Oracle7, the default width and format of unformatted DATE columns in SQL*Plus is derived from the NLS parameters in effect. Otherwise, the default width is A9. In Oracle7, the NLS parameters may be set in your database parameter file or may be environment variables or an equivalent platform-specific mechanism. They may also be specified for each session with the ALTER SESSION command. (See the documentation for the Oracle7 Server for a complete description of the NLS parameters).
You can change the format of any DATE column using the SQL function TO_CHAR in your SQL SELECT statement. You may also wish to use an explicit COLUMN FORMAT command to adjust the column width.
When you use SQL functions like TO_CHAR, Oracle automatically allows for a very wide column.
To change the width of a DATE column to n, use the COLUMN command with FORMAT An. If you specify a width shorter than the column heading, the heading is truncated.
NUMBER Columns To change a NUMBER column's width, use FORMAT followed by an element as specified in Table 6 - 1.
Element Example(s) Description
9 9999 Number of "9"s specifies number of significant digits returned. Blanks are displayed for leading zeroes and for a value of zero.
0 0999
9990
Displays a leading zero or a value of zero in this position as a 0, rather than as a blank.
$ $9999 Prefixes value with dollar sign.
B B9999 Displays a zero value as blank, regardless of "0"s in the format model.
MI 9999MI Displays "-" after a negative value. For a positive value, a trailing space is displayed.
S S9999 Returns "+" for positive values and "-" for negative values in this position.
PR 9999PR Displays a negative value in <angle brackets>. For a positive value, a leading and trailing space is displayed.
D 99D99 Displays the decimal character in this position, separating the integral and fractional parts of a number.
G 9G999 Displays the group separator in this position.
C C999 Displays the ISO currency symbol in this position.
L L999 Displays the local currency symbol in this position.
, (comma) 9,999 Displays a comma in this position.
. (period) 99.99 Displays a period (decimal point) in this position, separating the integral and fractional parts of a number.
V 999V99 Multiplies value by 10n, where n is the number of "9"s after the "V".
EEEE 9.999EEEE Displays value in scientific notation (format must contain exactly four "E"s).
RN or rn RN Displays upper- or lowercase Roman numerals. Value can be an integer between 1 and 3999.
DATE DATE Displays value as a date in MM/DD/YY format; used to format NUMBER columns that represent Julian dates.
Table 6 - 1. Number Formats

The MI and PR format elements can only appear in the last position of a number format model. The S format element can only appear in the first or last position.
If a number format model does not contain the MI, S or PR format elements, negative return values automatically contain a leading negative sign and positive values automatically contain a leading space.
A number format model can contain only a single decimal character (D) or period (.), but it can contain multiple group separators (G) or commas (,). A group separator or comma cannot appear to the right of a decimal character or period in a number format model.
SQL*Plus formats NUMBER data right-justified. A NUMBER column's width equals the width of the heading or the width of the FORMAT plus one space for the sign, whichever is greater. If you do not explicitly use FORMAT, then the column's width will always be at least the value of SET NUMWIDTH.
If a value does not fit within the column width, SQL*Plus indicates overflow by displaying a pound sign (#) in place of each digit the width allows.
If a positive value is extremely large and a numeric overflow occurs when rounding a number, then the infinity sign (~) replaces the value. Likewise, if a negative value is extremely small and a numeric overflow occurs when rounding a number, then the negative infinity sign replaces the value (-~).
With all number formats, SQL*Plus rounds each value to the specified number of significant digits as set with the SET NUMWIDTH command.
HEA[DING] text Defines a column heading. If you do not use a HEADING clause, the column's heading defaults to column or expr. If text contains blanks or punctuation characters, you must enclose it with single or double quotes. Each occurrence of the HEADSEP character (by default, '|') begins a new line. For example,

	COLUMN ENAME HEADING 'Employee |Name'
would produce a two-line column heading. See the HEADSEP variable of the SET command in this chapter for information on changing the HEADSEP character.
JUS[TIFY] {L[EFT]|C[ENTER]|C[ENTRE]|R[IGHT]} Aligns the heading. If you do not use a JUSTIFY clause, headings for NUMBER columns default to RIGHT and headings for other column types default to LEFT.
LIKE {expr|alias} Copies the display attributes of another column or expression (whose attributes you have already defined with another COLUMN command). LIKE copies only attributes not defined by another clause in the current COLUMN command.
NEWL[INE] Starts a new line before displaying the column's value. NEWLINE has the same effect as FOLD_BEFORE.
NEW_V[ALUE] variable Specifies a variable to hold a column value. You can reference the variable in TTITLE commands. Use NEW_VALUE to display column values or the date in the top title. You must include the column in a BREAK command with the SKIP PAGE action. The variable name cannot contain a pound sign (#).
NEW_VALUE is useful for master/detail reports in which there is a new master record for each page. For master/detail reporting, you must also include the column in the ORDER BY clause. See the example at the end of this command description.
For information on displaying a column value in the bottom title, see COLUMN OLD_VALUE. Refer to TTITLE for more information on referencing variables in titles. See COLUMN FORMAT for details on formatting and valid format models.
NOPRI[NT]|PRI[NT] Controls the printing of the column (the column heading and all the selected values). NOPRINT turns the printing of the column off. PRINT turns the printing of the column on.
NUL[L] text Controls the text SQL*Plus displays for null values in the given column. The default is a white space. SET NULL controls the text displayed for all null values for all columns, unless overridden for a specific column by the NULL clause of the COLUMN command.
OLD_V[ALUE] variable Specifies a variable to hold a column value. You can reference the variable in BTITLE commands. Use OLD_VALUE to display column values in the bottom title. You must include the column in a BREAK command with the SKIP PAGE action.
OLD_VALUE is useful for master/detail reports in which there is a new master record for each page. For master/detail reporting, you must also include the column in the ORDER BY clause.
For information on displaying a column value in the top title, see COLUMN NEW_VALUE. Refer to TTITLE for more information on referencing variables in titles.
ON|OFF Controls the status of display attributes for a column. OFF disables the attributes for a column without affecting the attributes' definition. ON reinstates the attributes.
WRA[PPED]|WOR[D_WRAPPED]|TRU[NCATED] Specifies how SQL*Plus will treat a CHAR, VARCHAR2, LONG, or DATE string that is too wide for a column. WRAPPED wraps the string within the column bounds, beginning new lines when required. When WORD_WRAP is enabled, SQL*Plus left justifies each new line, skipping all leading whitespace (for example, returns, newline characters, tabs and spaces), including embedded newline characters. Embedded whitespace not on a line boundary is not skipped. TRUNCATED truncates the string at the end of the first line of display.
Usage Notes

You can enter any number of COLUMN commands for one or more columns. All column attributes set for each column remain in effect for the remainder of the session, until you turn the column OFF, or until you use the CLEAR COLUMN command. Thus, the COLUMN commands you enter can control a column's display attributes for multiple SQL SELECT commands.

When you enter multiple COLUMN commands for the same column, SQL*Plus applies their clauses collectively. If several COLUMN commands apply the same clause to the same column, the last one entered will control the output.

Examples

To make the ENAME column 20 characters wide and display EMPLOYEE NAME on two lines at the top, enter

SQL> COLUMN ENAME FORMAT A20 HEADING 'EMPLOYEE |NAME'

To format the SAL column so that it shows millions of dollars, rounds to cents, uses commas to separate thousands, and displays $0.00 when a value is zero, you would enter

SQL> COLUMN SAL FORMAT $9,999,990.99

To assign the alias NET to a column containing a long expression, to display the result in a dollar format, and to display <NULL> for null values, you might enter

SQL> COLUMN SAL+COMM+BONUS-EXPENSES-INS-TAX ALIAS NET
SQL> COLUMN NET FORMAT $9,999,999.99 NULL '<NULL>'

Note that the example divides this column specification into two commands. The first defines the alias NET, and the second uses NET to define the format.

Also note that in the first command you must enter the expression exactly as you entered it (or will enter it) in the SELECT command. Otherwise, SQL*Plus cannot match the COLUMN command to the appropriate column.

To wrap long values in a column named REMARKS, you can enter

SQL> COLUMN REMARKS FORMAT A20 WRAP

For example:

CUSTOMER   DATE      QUANTITY REMARKS
---------- --------- -------- --------------------
123        25-AUG-86      144 This order must be s
                              hipped by air freigh
                              t to ORD

If you replace WRAP with WORD_WRAP, REMARKS looks like this:

CUSTOMER   DATE      QUANTITY REMARKS
---------- --------- -------- ---------------------
123        25-AUG-86      144 This order must be
                              shipped by air freight
                              to ORD

If you specify TRUNCATE, REMARKS looks like this:

CUSTOMER   DATE      QUANTITY REMARKS
---------- --------- -------- --------------------
123        25-AUG-86      144 This order must be s

In order to print the current date and the name of each job in the top title, enter the following. (For details on creating a date variable, see "Displaying the Current Date in Titles" under "Defining Page Titles and Dimensions"[*].)

SQL> COLUMN JOB NOPRINT NEW_VALUE JOBVAR
SQL> COLUMN TODAY  NOPRINT NEW_VALUE DATEVAR
SQL> BREAK ON JOB SKIP PAGE ON TODAY
SQL> TTITLE CENTER 'Job Report' RIGHT DATEVAR  SKIP 2 -
>    LEFT 'Job:     ' JOBVAR SKIP 2
SQL> SELECT TO_CHAR(SYSDATE, 'MM/DD/YY') TODAY,
  2  ENAME, JOB, MGR, HIREDATE, SAL, DEPTNO
  3  FROM EMP WHERE JOB IN ('CLERK', 'SALESMAN')
  4  ORDER BY JOB, ENAME;

Your two page report would look similar to the following report, with "Job Report" centered within your current linesize:

                    Job Report             08/01/94

Job:     CLERK

ENAME          MGR HIREDATE          SAL     DEPTNO
---------- ------- --------- ----------- ----------
ADAMS         7788 14-JAN-87        1100         20
JAMES         7698 03-DEC-81         950         30
MILLER        7782 23-JAN-82        1300         10
SMITH         7902 17-DEC-80         800         20
                    Job Report             08/01/94

Job:     CLERK 

ENAME          MGR HIREDATE          SAL     DEPTNO
---------- ------- --------- ----------- ----------
ALLEN         7698 20-JAN-81        1600         30
MARTIN        7698 03-DEC-81         950         30
MILLER        7782 23-JAN-82        1300         10
SMITH         7902 17-DEC-80         800         20

To change the default format of DATE columns to 'YYYY-MM-DD', you can enter

SQL> ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD';

The following output results:

Session altered

To display the change, enter a SELECT statement, such as:

SQL> SELECT HIREDATE
  2  FROM EMP
  3  WHERE EMPNO = 7839;

The following output results:

HIREDATE
----------
1981-11-17

See the Oracle7 Server SQL Language Reference Manual for information on the ALTER SESSION command.

Note that in a SELECT statement, some SQL calculations or functions, such as TO_CHAR, may cause a column to be very wide. In such cases, use the FORMAT option to alter the column width.


Contents Index Home Previous Next