Sub topic 1
Why Explicit Conversion Matters
Oracle stores values according to their data types. A NUMBER stores numeric data, a DATE stores date and time data, and a VARCHAR2 stores text. When an expression combines different types, Oracle may perform an implicit conversion, which is a conversion Oracle chooses and performs for you.
Implicit conversion can make a short query work, but its result may depend on session settings or on the contents of the data. An explicit conversion names the function and the format model, so the intended conversion is visible and predictable.
Prerequisites
This chapter assumes that you can write basic SELECT, WHERE, and ORDER BY clauses. You should also know the NUMBER, VARCHAR2, and DATE data types and be able to run SQL in an Oracle client.
What Oracle Converts Implicitly
Suppose order_ref is a VARCHAR2 column:
-- Risky: Oracle may convert order_ref to NUMBER
SELECT *
FROM orders
WHERE order_ref = 1001;
-- Clear: both sides are character values
SELECT *
FROM orders
WHERE order_ref = '1001';For the first query, Oracle generally converts the character column to a number because the other operand is numeric. If any row contains a value such as A-17, the conversion can fail with ORA-01722: invalid number. Applying a function to the column can also prevent Oracle from using an ordinary index on that column efficiently.
The second query compares text with text. It does not try to interpret every order_ref value as a number.
The same issue appears with dates. A condition such as this depends on the session's date format:
SELECT *
FROM employees
WHERE hire_date = '28-11-2026';Oracle must interpret the string as a date. If the session expects DD-MON-RR, or uses another language, the query can fail with ORA-01861 or ORA-01843.
Use TO_DATE when the input is text:
SELECT TO_DATE('28-11-2026', 'DD-MM-YYYY') AS hire_day
FROM dual;The format model tells Oracle exactly how to read each part of the string.
Why Session Settings Matter
Oracle's NLS settings control regional behavior such as date formats, month names, decimal characters, and group separators. NLS means National Language Support. Two sessions can run the same SQL and interpret an unformatted string differently.
| Input | Possible interpretation |
|---|---|
| `'01-02-2026'` | February 1 or January 2 |
| `'1,234.56'` | One thousand two hundred thirty-four point fifty-six, or invalid text |
| `'15-JAN-2026'` | Valid in English, possibly invalid in another language |
Explicit conversion reduces this dependency. For text intended for display, use TO_CHAR. For text received from a file or application, use TO_DATE or TO_NUMBER with a matching format model. When names or separators are involved, provide the relevant NLS parameter.
SELECT TO_DATE(
'15-JAN-2026',
'DD-MON-YYYY',
'NLS_DATE_LANGUAGE=ENGLISH'
) AS parsed_date
FROM dual;A Reliable Conversion Path
The safest pattern is to convert at the boundary where text enters the database, validate it, and then work with the resulting native type.
flowchart TD
A[Text from a file or application] --> B[Explicit conversion with a format model]
B --> C{Conversion succeeds?}
C -->|Yes| D[Use DATE or NUMBER in SQL]
C -->|No| E[Reject, report, or handle the bad value]Explicit conversion also makes errors easier to diagnose. ORA-01722 usually indicates invalid numeric text. ORA-01843 often indicates an invalid month. ORA-01861 means the input does not match the expected format. Find the conversion boundary, inspect the input value, and compare it with the format model.
For date filters, convert constants and leave the indexed column untouched:
SELECT *
FROM employees
WHERE hire_date >= TO_DATE('2026-01-01', 'YYYY-MM-DD')
AND hire_date < TO_DATE('2027-01-01', 'YYYY-MM-DD');This half-open range includes every time on the dates in 2026 without wrapping hire_date in a function.
Exercise
Why is WHERE order_ref = '1001' safer than WHERE order_ref = 1001 when order_ref is VARCHAR2?
Show answer
The quoted form compares text with text. The numeric form can make Oracle convert every column value to a number, causing ORA-01722 for nonnumeric rows and potentially harming index use.