Sub topic 6
Implicit conversions that disable an index
Assume you know basic SELECT and WHERE syntax and understand Oracle types such as NUMBER, VARCHAR2, DATE, and TIMESTAMP. You should also know that an index stores values in an order that Oracle can search without reading every table row.
An implicit conversion is a type conversion that Oracle adds automatically when an expression compares different data types. For example, a VARCHAR2 column compared with a number may cause Oracle to apply TO_NUMBER to the column:
SELECT customer_id
FROM customers
WHERE phone_number = 5551234;If phone_number is VARCHAR2, Oracle may evaluate this as:
TO_NUMBER(phone_number) = 5551234The function is applied to every candidate column value. A normal index on phone_number contains the text values, not their converted numeric values, so Oracle generally cannot use that index for an ordinary range or equality scan. The query can also fail with ORA-01722: invalid number if any row contains nonnumeric text.
Match the literal to the column type
Use a character literal when the column is VARCHAR2:
SELECT customer_id
FROM customers
WHERE phone_number = '5551234';Now Oracle compares phone_number directly with a character value. An index on phone_number can be used, and values such as '05551234' remain distinct from '5551234'.
The same principle applies to dates. Avoid comparing a DATE column with a formatted string whose meaning depends on the session's NLS_DATE_FORMAT setting:
-- Depends on session settings
SELECT order_id
FROM orders
WHERE order_date = '15-JAN-2026';
-- Explicit and independent of session settings
SELECT order_id
FROM orders
WHERE order_date >= DATE '2026-01-15'
AND order_date < DATE '2026-01-16';The second form also handles an order_date value that includes a time. Equality against midnight would not match rows later on the same day.
| Column type | Safer comparison value |
|---|---|
| `VARCHAR2` | A quoted character literal or a character bind variable |
| `NUMBER` | A numeric literal or a numeric bind variable |
| `DATE` | An ANSI date literal such as `DATE '2026-01-15'` |
Bind variables and joins
Bind variables should have the same data type as the column they compare with. A client that binds a phone number as a number can recreate the same problem even when the SQL text looks correct.
Mismatched types in joins are another warning sign:
SELECT o.order_id, c.customer_id
FROM orders o
JOIN customers c
ON o.customer_id = c.customer_code;If customer_id is NUMBER and customer_code is VARCHAR2, Oracle must convert one side. Align the column types in the schema when possible. If that is impossible, convert the smaller or controlled input before the comparison, and verify the plan. Converting an indexed column can prevent efficient index access.
How to recognize the problem
A plan can reveal the conversion. Look for predicates such as TO_NUMBER("PHONE_NUMBER") or TO_CHAR("CUSTOMER_ID") instead of a direct comparison on the indexed column.
Check the execution plan with DBMS_XPLAN after correcting the SQL. The predicate should compare the indexed column directly with a value of the matching type.
Exercise
PHONENUMBER is VARCHAR2, and an index exists on it. Which predicate is index-friendly: phonenumber = 5551234 or phone_number = '5551234'?
Show answer
phone_number = '5551234'. The quoted literal has the same character type, so Oracle does not need to convert the indexed column to a number.