Sub topic 1
Why explicit conversion matters
Oracle converts between character strings, numbers, and dates for you whenever a statement mixes them. This chapter explains when that happens, why it is a risk in production code, and what to do instead. The later chapters cover the functions in detail: TO_CHAR, TO_DATE, TO_NUMBER, and the format models they use.
Prerequisites
This chapter assumes you can:
- Write basic
SELECT,WHERE, andORDER BYstatements. - Tell the
NUMBER,VARCHAR2, andDATEdata types apart. - Run SQL in SQL*Plus, SQL Developer, or a similar client.
- Use the
DUALtable for one-row queries.
You do not need to know format models yet. The first examples use only simple ones, and the next chapter covers them in full.
Two kinds of conversion
A data type conversion changes a value from one type to another. For example, it turns the string '2026-11-28' into a DATE, or the number 1234.5 into the text '1234.5'. Oracle does this in two ways.
- Implicit conversion happens automatically. Oracle sees a type mismatch and applies a conversion on its own, using default rules.
- Explicit conversion happens because you wrote a conversion function such as
TO_CHAR,TO_DATE, orTO_NUMBER, or usedCAST. You choose the target type and, usually, the format.
Implicit conversion is convenient, so most people meet it first without noticing. The next sections show the cost of that convenience.
Where implicit conversion happens
Oracle applies implicit conversion in several common situations:
| Situation | Example | What Oracle does | Column | Column |
|---|---|---|---|---|
| Comparing a `DATE` column with a string | `hire_date > '01-02-2026'` | Converts the string to a `DATE` using the session's `NLS_DATE_FORMAT` | ||
| Comparing a `VARCHAR2` column with a number | `order_ref = 1001` | Converts the column values to numbers | ||
| Concatenating a number or date with text | `'Total: ' \ | \ | 1234.5` | Converts the number to text using the session's numeric characters |
| Inserting a string into a `DATE` column | `INSERT ... VALUES ('03-04-2026')` | Converts the string with `NLS_DATE_FORMAT` | ||
| Arithmetic on a string | `'10' + 5` | Converts the string to a number |
NLS stands for National Language Support. NLS parameters are session-level settings that control language, date format, and number separators. NLS_DATE_FORMAT is the format Oracle uses when it converts between dates and strings without being told a format. NLS_NUMERIC_CHARACTERS sets the decimal and group separator characters.
These settings are not fixed. They can differ between database instances, client programs, and users. They come from the database defaults, the client's NLS_LANG environment setting, or an ALTER SESSION statement. Some tools also set their own values when they connect. Code that relies on them behaves differently depending on where it runs.
Risk 1: the result depends on the session
Here is the same statement run under two session settings. CAST(... AS DATE) with no format is an implicit conversion of the string. The TO_CHAR around it, with an explicit format, makes the output readable.
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MM-YYYY';
SELECT TO_CHAR(CAST('03-04-2026' AS DATE), 'YYYY-MM-DD') AS day_first
FROM dual;
ALTER SESSION SET NLS_DATE_FORMAT = 'MM-DD-YYYY';
SELECT TO_CHAR(CAST('03-04-2026' AS DATE), 'YYYY-MM-DD') AS month_first
FROM dual;The first query returns 2026-04-03 and the second returns 2026-03-04. The input string is identical, but the dates are a month apart. Nothing fails, so you get no warning that the data is wrong.
Numbers have the same problem in the other direction. Concatenation converts a number to text with no grouping and with the session's decimal character:
ALTER SESSION SET NLS_NUMERIC_CHARACTERS = '.,';
SELECT 'Total: ' || 1234.5 AS txt FROM dual;
ALTER SESSION SET NLS_NUMERIC_CHARACTERS = ',.';
SELECT 'Total: ' || 1234.5 AS txt FROM dual;The first query returns Total: 1234.5 and the second returns Total: 1234,5. If a report writes that text to a file for another system to read, the output changes whenever the session settings change.
An explicit conversion removes the dependency. You state the format, and optionally the NLS settings, in the statement itself:
SELECT TO_CHAR(TO_DATE('03-04-2026', 'DD-MM-YYYY'), 'YYYY-MM-DD') AS parsed
FROM dual;This returns 2026-04-03 in every session.
Risk 2: errors depend on the data
Consider a table where order_ref is a VARCHAR2 column and some references are purely numeric. A developer writes:
-- Fails with ORA-01722 if any row of order_ref holds a non-numeric value
SELECT * FROM orders WHERE order_ref = 1001;When a VARCHAR2 value is compared with a number, Oracle converts the column to a number, not the number to a string. Effectively it runs TO_NUMBER(order_ref) = 1001. If any row that Oracle examines contains a value such as 'A-77', the query stops with ORA-01722: invalid number.
This kind of bug is hard to find for three reasons:
- The query works in testing, where every
order_refhappens to be numeric. - It fails in production only after someone inserts a non-numeric reference.
- Whether it fails can depend on the execution plan, because Oracle might filter out the bad rows with another predicate before it converts them. A plan change after statistics are gathered can turn a working query into a failing one.
The safe version compares text with text:
-- Explicit and safe: compare as text
SELECT * FROM orders WHERE order_ref = '1001';Now Oracle compares strings and never tries to read 'A-77' as a number.
Risk 3: lost indexes
An implicit conversion that lands on a column is the same as wrapping the column in a function. A regular B-tree index on order_ref stores the text values, so it cannot answer a condition on TO_NUMBER(order_ref). Oracle then has to read the table and convert every value.
The direction of the conversion decides the outcome:
- If the literal is converted, the column is left alone and its index remains usable.
- If the column is converted, a regular index on that column generally cannot be used.
The next diagram shows how Oracle chooses the direction when you compare a column with a literal of a different type.
Even the good branch has a catch. When the string literal is converted to a DATE, the column is untouched and the index works, but the string is still read with the session's NLS_DATE_FORMAT. That brings back Risk 1. Writing the conversion yourself solves both problems:
SELECT *
FROM employees
WHERE hire_date >= TO_DATE('2026-01-01', 'YYYY-MM-DD');A later chapter looks at index-friendly filters in more detail.
Risk 4: code that hides its intent
A reader cannot see an implicit conversion. Take this condition:
WHERE created_on > '01-02-2026'Does it mean 1 February or 2 January? The answer is not in the code. It depends on a setting somewhere else. When you write TO_DATE('01-02-2026', 'DD-MM-YYYY'), the intent is in the statement, so a reviewer can check it and a future maintainer cannot misread it.
How to convert explicitly
You have three tools, and each fits a different job.
- Conversion functions with a format model. Use
TO_CHARto format a date or number as text, andTO_DATEandTO_NUMBERto parse text. The format model tells Oracle how the text is laid out. This is the main approach and the subject of the next chapters. - Date and timestamp literals. When the value is a constant in your SQL, write it in the ANSI form, which does not depend on any NLS setting:
``sql SELECT DATE '2026-11-28' AS d, TIMESTAMP '2026-11-28 14:30:15.123456' AS ts FROM dual; ``
The DATE literal always uses YYYY-MM-DD. The TIMESTAMP literal uses YYYY-MM-DD HH24:MI:SS[.FF].
CAST.CAST(value AS type)is standard SQL and converts to a target type, but it has no format model argument. Strings are therefore read with the session defaults. For dates and numbers, prefer theTO_functions.
Pass the NLS settings you depend on as well. Several TO_ functions accept a final nlsparam argument, for example NLS_DATE_LANGUAGE=ENGLISH when parsing month names. With it, the statement no longer depends on the session's language setting. Later chapters cover that argument.
When implicit conversion is acceptable
Implicit conversion is not always wrong. Oracle documents the rules, and some uses are harmless. Comparing a NUMBER column with '1001' converts the literal, not the column. Quick ad hoc queries in a SQL client, where you can see the output, also carry little risk.
Stored code is different: views, PL/SQL packages, reports, scheduled jobs, and application SQL. It runs under settings you do not control and keeps running for years. In that code, treat every type mismatch as a defect and make the conversion explicit.
Summary of the rules
- Implicit conversion uses session-dependent defaults, so identical SQL can give different results, or errors, in different environments.
- A
VARCHAR2column compared with a number is converted to a number. This can raiseORA-01722and prevent index use. - When Oracle converts a literal and leaves the column alone, indexes stay usable, but the literal is still interpreted using session settings.
- Use
TO_CHAR,TO_DATE, andTO_NUMBERwith explicit format models, or ANSIDATEandTIMESTAMPliterals, in any code that is stored or reused.
Exercise
A table invoices has a VARCHAR2(20) column invoice_no with a regular index. The query SELECT * FROM invoices WHERE invoice_no = 20260101 works in the test database but raises ORA-01722 in production, and in both databases it ignores the index. Explain both symptoms and give the fix.
Show answer
Because the column is VARCHAR2 and the literal is a number, Oracle converts the column to a number, as if the condition were TONUMBER(invoiceno) = 20260101. In production some row holds a non-numeric value such as 'INV-17', so the conversion raises ORA-01722. The test data is all numeric, so it passes there. Wrapping the column in a conversion also stops a regular index on invoiceno from being used in both databases. The fix is to compare text with text: WHERE invoiceno = '20260101'. Oracle then converts nothing, never reads bad values as numbers, and can use the index.
Exercise
A colleague's report contains WHERE order_date >= '05-06-2026'. It returns rows from 5 June on one server and from 6 May on another. Why does this happen, and how would you rewrite the condition so it always means 5 June 2026?
Show answer
The string literal is converted to a DATE implicitly, using the session's NLSDATEFORMAT. One server uses DD-MM-YYYY and the other uses MM-DD-YYYY, so the same text means different dates. Rewrite it with an explicit format: WHERE orderdate >= TODATE('05-06-2026', 'DD-MM-YYYY'), or use the ANSI literal WHERE order_date >= DATE '2026-06-05'. Both give the same result in every session.