| Lesson 5 | Formatting Numeric Columns |
| Objective | Control the display of numeric columns. |
COLUMN command, already familiar from the previous two lessons, is the primary tool for numeric formatting as well. Applied to a specific column, it looks like this:
COLUMN salary FORMAT 999,999.99
Here, salary is the column name and 999,999.99 is the format model: a template built from the special characters covered in detail below.
SET NUMFORMAT:
SET NUMFORMAT 9,999.99
A specific COLUMN FORMAT setting always wins over a session-wide SET NUMFORMAT default; think of COLUMN FORMAT as the more specific, and therefore higher-priority, instruction. There is a third layer beneath both of these worth knowing about: SET NUMWIDTH, which sets the default column width used when neither COLUMN FORMAT nor SET NUMFORMAT has been applied at all. The precedence runs in exactly that order: COLUMN FORMAT first, then SET NUMFORMAT, then SET NUMWIDTH as the final fallback, whose own default value is 10 characters.
| Character | Description | Format specification | Input value | Displayed output |
|---|---|---|---|---|
| 9 | Represents a digit; leading zeros are suppressed and shown as blanks. | 999 999999 |
123 123 |
123 123 |
| 0 | Same as 9, but does not suppress leading or trailing zeros. | 099 099999 990999 |
123 123 123 |
123 000123 0123 |
| . or D | Marks the decimal point. A format model can contain only one decimal character. | 999.99 | 123.45 | 123.45 |
| , or G | Marks a group separator. Multiple group separators are allowed, but none may appear to the right of the decimal point. | 999,999.99 | 123456.78 | 123,456.78 |
| $ | Places a leading dollar sign. | $999,999.99 | 123456.78 | $123,456.78 |
| L, C, U | Currency symbols: L for the local currency symbol, C for the ISO currency symbol, U for the dual currency symbol. | L999 C999 U999 |
123 123 123 |
(locale-dependent symbol)123 |
| MI | Displays a trailing minus sign after a negative value, a trailing space after a positive one. Can only appear in the last position of a format model. | 9999MI | -123 123 |
123- 123 |
| PR | Displays a negative value in angle brackets, a positive value with a leading and trailing space. Can only appear in the last position of a format model. | 9999PR | -123 123 |
<123> 123 |
| S | Forces a sign to display for both positive and negative values. Can only appear in the first or last position of a format model. | S9999 9999S |
123 / -123 123 / -123 |
+123 / -123 123+ / 123- |
| B | Displays blanks for the integer part of a fixed-point number when that integer part is zero, regardless of any zeros in the format model. | B9999.99 | 0.45 | .45 |
| V | Multiplies the value by 10 raised to the number of 9s following the V. | 999V99 | 1.23 | 00123 |
| X or x | Displays the hexadecimal value of the rounded number, uppercase or lowercase. | XXXX | 255 | FF |
| RN or rn | Displays Roman numerals, uppercase or lowercase, for integers between 1 and 3999. | RN | 14 | XIV |
| EEEE | Scientific notation. The format model must contain exactly four E characters. | 9.99EEEE | 12345 | 1.23E+04 |
| TM | Displays the smallest number of characters possible. Defaults to TM9. Cannot be preceded by any other element; can only be followed by a single 9 or E. | TM | 123.4500 | 123.45 |
SQL> COLUMN a FORMAT 999,999.99
SQL> COLUMN b FORMAT 099,999.99
SQL> COLUMN c FORMAT $999,999.99
SQL> SELECT 123.45 a,
2 234.56 b,
3 345.67 c
4 FROM dual;
A B C
------------ ------------ --------------
123.45 000,234.56 $345.67
Each column receives its own value, formatted according to its own rule: A shows 123.45 plainly, with no leading zeros needed. B shows 234.56 with leading zeros filled in per the 0 elements in its format. C shows 345.67 with a leading dollar sign. Note that this corrects an error that has appeared in some older versions of this example, where columns B and C were shown displaying column A's value instead of their own.