Sub topic 12
Conversions in WHERE Clauses and Index Use
This chapter assumes you know basic SELECT and WHERE syntax, Oracle NUMBER, VARCHAR2, and DATE types, and the purpose of a regular index. The examples focus on how conversion expressions affect filtering and index access.
Why conversions affect indexes
An index stores column values in an ordered structure. Oracle can often find matching rows quickly when a predicate compares the indexed column directly with a compatible value:
SELECT employee_id, last_name, hire_date
FROM employees
WHERE hire_date >= DATE '2026-01-01'
AND hire_date < DATE '2027-01-01';The hire_date column remains unchanged. Oracle can use a normal index on hire_date to find the range of dates.
A function applied to the column changes the expression Oracle must evaluate:
SELECT employee_id, last_name, hire_date
FROM employees
WHERE TO_CHAR(hire_date, 'YYYY') = '2026';This query formats every candidate hire_date as text before comparing it. A normal index on hire_date usually cannot support this search efficiently because the index contains dates, not the results of TO_CHAR(hire_date, 'YYYY').
Use a range instead. A half-open range includes the start date and excludes the next boundary:
SELECT employee_id, last_name, hire_date
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 also handles the time portion of a DATE. A condition such as hire_date <= TO_DATE('2026-12-31', 'YYYY-MM-DD') can accidentally exclude rows on December 31 after midnight.
Avoiding implicit conversion
Oracle may convert one data type automatically when a comparison uses different types. This is called implicit conversion. The direction depends on the data types and expression, so do not rely on it.
Suppose order_ref is a VARCHAR2 column:
-- Risky: Oracle may convert order_ref to NUMBER
SELECT *
FROM orders
WHERE order_ref = 1001;
-- Explicit: compare text with text
SELECT *
FROM orders
WHERE order_ref = '1001';If any order_ref contains a value such as 'A-1001', the first query can fail with ORA-01722: invalid number. Converting the column can also prevent efficient use of an index on order_ref.
When the input is genuinely numeric but arrives as text, convert the input explicitly:
SELECT *
FROM orders
WHERE order_ref = TO_CHAR(:order_id);Here :order_id is a bind variable. The conversion occurs on the value being supplied, while the column remains available for index access.
Common errors and diagnosis
Conversion errors often indicate that the format model does not match the data:
| Error | Typical cause | Check |
|---|---|---|
| `ORA-01722` | Non-numeric text converted to `NUMBER` | Inspect invalid character values |
| `ORA-01843` | Unrecognized month or language mismatch | Check month names and NLS language |
| `ORA-01861` | Input does not match the format model | Compare every input element with the model |
Use explicit format models and consistent data types. If a query still performs poorly, inspect its execution plan with DBMS_XPLAN.DISPLAY_CURSOR and check whether Oracle converted the indexed column or used a function-based access path.
Exercise
Rewrite WHERE TO_CHAR(hire_date, 'YYYY-MM') = '2026-11' as an index-friendly predicate.
Show answer
Use WHERE hire_date >= DATE '2026-11-01' AND hire_date < DATE '2026-12-01'.