Sub topic 8
Parsing Strings with `TO_NUMBER`
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.
A character string is text. A number is a numeric value that Oracle can add, compare, and sort numerically. TO_NUMBER explicitly converts text into a NUMBER value:
SELECT TO_NUMBER('1234.56') AS amount
FROM dual;The input must contain a valid number for the current session settings. Explicit conversion is safer than implicit conversion because the query states exactly how Oracle should interpret the text.
Matching the format model
A format model is a pattern that tells Oracle how characters represent a number. The model must match the input string, including grouping separators and decimal positions.
| Element | Meaning | Example |
|---|---|---|
| `9` | Digit position, with optional leading blanks | `9999` |
| `0` | Digit position, including required zeroes | `0000` |
| `G` | Grouping separator | `9G999` |
| `D` | Decimal separator | `999D99` |
| `S` | Sign position | `S9999` |
For example, this input uses a comma for grouping and a period for the decimal point:
SELECT TO_NUMBER('1,234.56', '9,999.99') AS us_style
FROM dual;The result is the number 1234.56, not a string. You can use it in arithmetic:
SELECT TO_NUMBER('1,234.56', '9,999.99') * 1.10 AS amount_with_tax
FROM dual;The format model should describe the input, not the display you want later. Use TO_CHAR when you need to format a number as text.
Controlling separators with `nlsparam`
Oracle's NLS settings control locale-dependent behavior. NLS means National Language Support. For numbers, the relevant settings include the decimal character and the grouping character.
Do not rely on the session's settings when input can arrive in different formats. Use G, D, and the nlsparam argument to define the separators:
SELECT TO_NUMBER(
'1.234,56',
'9G999D99',
'NLS_NUMERIC_CHARACTERS='',.'''
) AS eu_style
FROM dual;Here, the nlsparam value says that the decimal character is a comma and the grouping character is a period. Both calls below return the same numeric value:
SELECT TO_NUMBER('1,234.56', '9,999.99') AS us_style,
TO_NUMBER(
'1.234,56',
'9G999D99',
'NLS_NUMERIC_CHARACTERS='',.'''
) AS eu_style
FROM dual;A currency symbol can also be included with a suitable format element such as L, which uses the session currency. If the input format is controlled by an external system, define the expected currency and separators explicitly rather than assuming the database session's locale.
Diagnosing invalid input
An invalid numeric string raises ORA-01722: invalid number. Typical causes include letters, misplaced separators, or a format model that does not match the input.
-- Raises ORA-01722
SELECT TO_NUMBER('12x')
FROM dual;
-- The format model does not match the comma-separated input
SELECT TO_NUMBER('1,234.56', '9999.99')
FROM dual;Implicit conversion can produce the same error in a less obvious place. If order_ref is a VARCHAR2 column, this predicate may make Oracle convert every value in the column to a number:
-- Risky when order_ref contains non-numeric text
SELECT *
FROM orders
WHERE order_ref = 1001;
-- Compare text with text when order_ref is a character column
SELECT *
FROM orders
WHERE order_ref = '1001';Handling untrusted input
Oracle Database 12.2 and later supports a fallback value when conversion fails:
SELECT TO_NUMBER('abc' DEFAULT -1 ON CONVERSION ERROR) AS amount,
VALIDATE_CONVERSION('12x' AS NUMBER) AS is_valid
FROM dual;The first expression returns -1. VALIDATE_CONVERSION returns 1 for valid input and 0 for invalid input. Choose a fallback that cannot be confused with a legitimate value, or return NULL and handle missing values separately.
Exercise
What value does this expression return, and why? TO_NUMBER('1.234,56', '9G999D99', 'NLS_NUMERIC_CHARACTERS='',.''')
Show answer
It returns the NUMBER value 1234.56 because the format uses G for the period grouping character and D for the comma decimal character.