Sub topic 4
Formatting Numbers with TO_CHAR in Oracle
This chapter assumes you know basic SELECT syntax and the NUMBER and VARCHAR2 data types. You should also be able to run queries against Oracle's DUAL table.
What TO_CHAR does
TO_CHAR converts a number into a character string. The number remains numeric in the database. Only the returned value is formatted.
This distinction matters because formatted text is useful for reports and user interfaces, but it is not suitable for arithmetic or numeric sorting.
SELECT TO_CHAR(1234567.891, '9,999,999.00') AS padded,
TO_CHAR(1234567.891, 'FM9,999,999.00') AS trimmed,
TO_CHAR(0.5, 'FM990.00') AS leading_zero
FROM dual;The result is similar to this:
| PADDED | TRIMMED | LEADING_ZERO |
|---|---|---|
| 1,234,567.89 | 1,234,567.89 | 0.50 |
The first format has a leading blank. Oracle reserves space for a positive or negative sign. The FM modifier removes that padding.
Reading a number format model
A format model is a pattern that tells Oracle how to display a number.
| Element | Meaning | Example |
|---|---|---|
| `9` | Optional digit position | `9999` |
| `0` | Required digit position | `0000` |
| `,` | Grouping separator | `1,234` |
| `.` | Decimal position | `12.50` |
| `G` | Locale-aware grouping separator | `1.234` |
| `D` | Locale-aware decimal separator | `12,50` |
| `L` | Local currency symbol | `$` or `€` |
| `FM` | Suppresses padding | `FM999.00` |
A 9 displays a digit when one exists. A 0 displays a zero even when the number has no digit in that position. The model FM990.00 therefore formats 0.5 as 0.50.
If the number needs more integer positions than the model provides, Oracle can return a row of # characters. Choose a model wide enough for the largest expected value.
SELECT TO_CHAR(9876.5, 'FM999,999.00') AS amount,
TO_CHAR(9876.5, 'FML999,999.00') AS local_amount
FROM dual;The L element uses the currency configured for the session. Use a literal currency symbol only when the output must always use that exact symbol.
Padding, rounding, and negative numbers
TO_CHAR rounds fractional values to the number of decimal places in the model. For example, 123.456 formatted with FM9990.00 becomes 123.46.
Negative numbers normally include a minus sign. You can control the sign with elements such as S, MI, or PR.
SELECT TO_CHAR(-42.5, 'FM9990.00') AS default_sign,
TO_CHAR(-42.5, 'FM9990.00MI') AS trailing_sign,
TO_CHAR(-42.5, 'FM9990.00PR') AS angle_brackets
FROM dual;Use FM when the formatted value will be placed directly into a label, CSV field, or report column. Keep padding when a fixed-width report requires aligned fields.
Separators and NLS settings
NLS settings control language, currency, and numeric conventions for an Oracle session. NLS means National Language Support.
G represents the grouping separator and D represents the decimal separator. You can specify the separators for one conversion with the nlsparam argument:
SELECT TO_CHAR(1234567.89,
'FM9G999G999D00',
'NLS_NUMERIC_CHARACTERS='',.''') AS eu_style
FROM dual;This returns 1.234.567,89, regardless of the session's usual numeric characters.
A common mistake
Do not store the result of TO_CHAR in a numeric column or compare formatted text as though it were a number. Text sorting puts values such as 100 before 20 because it compares characters, not numeric magnitude.
Exercise
Which format returns 7.5 as 7.50 without a leading blank: 9.99, FM90.00, or FM999?
Show answer
FM90.00. The 0 requires two decimal places, and FM removes padding.