The fix for an "invalid number" error is almost always to find the value that isn't numeric, then either make it numeric or move the conversion out of the way. In Oracle, the message is ORA-01722: invalid number. The database tried to convert a string to a number—usually because a column, literal, or bind variable was compared to a numeric column, or an explicit TO_NUMBER call received a non-numeric character—and failed. The key condition: this is a conversion problem, not a data-loss problem, so nothing is corrupted; a statement is simply refusing to run until its types agree.
That distinction matters because the error often appears in a program that "worked yesterday." Usually a new row, a new parameter, or a new locale introduced a character the conversion can't handle: a currency symbol, a comma, a stray space, or a letter in what should be a numeric field. This article walks through the diagnostic order that works, the specific traps in Oracle's implicit conversion rules, and how to write queries and code that stop producing the error in the first place.
Recognize the error and its close relatives first
Before touching anything, confirm what you're actually seeing. ORA-01722 is Oracle's message for a failed string-to-number conversion. Other engines use different names and different mechanics, so matching the message to the engine prevents you from applying the wrong fix.
In Oracle, the error number is specific: ORA-01722, "invalid number." It surfaces during parsing or execution of a statement that requires numeric evaluation. SQL Server is looser—it will happily convert many strings to numbers—but raises "Conversion failed when converting the varchar value ... to data type int" (or a similar type name) when it hits a non-numeric value. PostgreSQL refuses implicit conversion of text to numeric entirely unless you cast explicitly, so you'd see something like "invalid input syntax for type integer." MySQL converts aggressively and typically produces warnings rather than hard errors unless strict mode is on.
The practical point: ORA-01722 is the canonical "invalid number" error, so if you're on Oracle, read every mention below as directly applicable. If you're elsewhere, the diagnostic logic still holds—find the string that isn't a number—but the error text and the rules about when conversion happens differ.
One more distinction to get right up front: ORA-01722 is not ORA-06502 (numeric or value error, usually from PL/SQL variable overflow) and not ORA-01858 (non-numeric character where a date format expects a digit). Those are adjacent failures with different causes. If your error stack says something else, stop and read the actual message before proceeding.
Read the error context before you change anything
The single most useful step is often the one people skip: capture the full error context. Oracle error messages carry a position, and tools expose it differently.
Check the error position marker. If the error came with an asterisk pointing at a character position beneath the offending SQL text, that position is a strong hint about which expression failed. It's not always exact—implicit conversion can be reported at the comparison rather than the value—but it narrows the field from "somewhere in this 400-line query" to "around here."
Look for the failing SQL in the stack. Application error logs usually include the statement that failed, sometimes with bind variable values. If your framework logs the SQL and parameters separately, assemble them. A query that fails with one parameter set and succeeds with another tells you the problem is in the data, not the syntax.
Reproduce it in isolation. Run the smallest version of the failing statement in a SQL client. If SELECT * FROM orders WHERE order_id = 'ABC' fails, you've reproduced it in one line. If it only fails inside a 12-table join, peel back the joins one at a time—conversion errors sometimes come from a filter on a column you didn't realize was a VARCHAR2.
Note whether it's deterministic. Does the same statement fail every time, or only for certain inputs? Intermittent failures almost always trace to specific rows or parameter values, which is a much faster path than auditing the whole table.
A cheap way to check success here: once you can state "this exact statement with these exact bind values fails, and these succeed," you've localized the problem to a value or expression, and the rest of the process is mechanical.
Find the mismatched value with targeted queries
Now you know which statement fails. The goal is to find the string that can't convert. Work from broad to narrow.
Start with the obvious predicate. If the failure is in WHERE numeric_col = :bind, test the bind value directly: SELECT TO_NUMBER(:bind) FROM dual; with the actual value substituted. If that throws ORA-01722, you've found your culprit—the parameter isn't a clean number.
Audit the column that's implicitly converting. The classic pattern is a VARCHAR2 column compared against a number. Given WHERE varchar_col = 100, Oracle converts varchar_col to a number for every row, and one non-numeric row kills the query. Find the offending rows without triggering the error by testing whether the value is numeric using a pattern or a conversion guarded by a check. A common approach is to select the rows and apply a validation expression only to rows you care about, or to identify non-numeric values with a regular expression such as a test that the string contains only digits, a leading sign, and at most one decimal point. Run that filter first; the rows it returns are your suspects.
Check the columns you didn't think were strings. Join predicates are frequent offenders: a.customer_id = b.customer_id where one side is NUMBER and the other VARCHAR2. The optimizer converts the string side per row. Any row with a non-numeric customer_id breaks the join. This pattern is maddening because the columns look equivalent by name.
Suspect functions that return strings. A column that's numerically stored but wrapped in a function that returns VARCHAR2 (or a CASE expression mixing types) can flip the comparison into a string context. Implicit conversion then bites. Examine the expression types in your SELECT list, WHERE clause, and JOIN conditions.
Watch for the empty-string-to-NULL trap. Oracle treats an empty string '' as NULL, and NULL doesn't fail numeric conversion—it yields NULL, and WHERE numeric = NULL matches nothing. So empty strings are usually not the cause, but a value like ' ' (a single space) or '12 ' (trailing space) may or may not convert depending on the context. Don't assume; test the exact value.
The success check: you can name the specific value and the specific expression that fail together. If you have a column full of mixed data, a quick SELECT DISTINCT of the suspects, filtered to non-numeric-looking entries, will usually expose the bad format in seconds.
Fix the data, the query, or both—in that order of preference
Once you've localized the value, you have three interventions. Choose deliberately.
Option one: correct the data. If a numeric column should contain only numbers and a row has '1,234' or '$45.00' or '12kg', the right fix is usually to store the number and keep the formatting out of the database. Clean the row (strip formatting, store the numeric value) and add a check constraint or validation to prevent recurrence. This is the most durable fix when the column genuinely represents a number. The catch: you may not control the source system, and cleaning data upstream of the database is sometimes the only sustainable place.
Option two: correct the query. If the column legitimately stores mixed text—say, an identifier like 'A100' in one table and '100' in another—you may need to change how you compare. Cast explicitly where the data is known-clean: TO_NUMBER(clean_col). Or cast the numeric side to a string instead, which changes the semantics: WHERE varchar_col = TO_CHAR(100) compares strings, so '100' matches but ' 100' and '100.0' may not. That's often the safer move because it never attempts an unsafe conversion. Choose based on which side you can guarantee is well-formed.
Option three: guard the conversion. When neither side is guaranteed clean, apply the conversion conditionally. Compute a numeric value only for rows you've verified are numeric, or use a CASE expression that returns NULL (or a default) for malformed input. This puts the validation logic in the query, which is more complex but robust against future bad rows. It's the right call for reporting queries against messy data where you can't fix the source.
A worked example makes the trade-off concrete. Suppose orders has a VARCHAR2 column amount_text holding values like '250.00', '89.95', and one bad row 'N/A'. A query WHERE amount_text > 100 triggers ORA-01722 on the bad row. Casting the other side to text—WHERE amount_text > TO_CHAR(100)—compares strings, so '89.95' > '100' evaluates as true berween the two texts and gives you a wrong answer silently, which is worse than the error. The better fix is to identify and clean the 'N/A' row, then convert the column to NUMBER in the schema. The error was telling the truth: the column isn't a number.
Understand why implicit conversion makes this so slippery
The reason ORA-01722 surprises experienced developers is that Oracle's implicit conversion rules depend on the direction of the comparison, and the direction isn't always obvious.
Rule of thumb: the string is usually converted to a number, not the reverse. When you compare a NUMBER column to a VARCHAR2 expression, Oracle's documented behavior is to convert the character value to NUMBER and compare numerically. That's why a single bad string poisons the whole query. The conversion happens for rows you don't even care about, because the predicate is evaluated before your other filters (or in an order you don't control).
But it's not universal. In some expressions, a number gets converted to a string—particularly when a function returns VARCHAR2 or when data types are mixed in specific ways. The practical takeaway is to never rely on remembering the rule. Read the expression and assume the worse case: a string may be parsed as a number.
Predicate evaluation order is not guaranteed. You might write WHERE is_numeric_flag = 'Y' AND value_text > 100, expecting the first condition to filter out bad rows before the second converts them. The optimizer may reorder the predicates. This is the most common cause of "but I guarded it!" failures. The reliable fix is to make the conversion itself safe—use a CASE expression or filter the clean data into a subquery or CTE and convert outside it. Testing whether a subquery reliably isolates the conversion is worth doing once on your version, because optimizer transformations can still flatten it.
Data type precedence matters across engines. SQL Server uses data type precedence to decide which operand converts, and INT outranks VARCHAR, so a string is converted to int—same trap, different name. PostgreSQL avoids the whole class of bug by refusing implicit text-to-numeric conversions. If you're porting code between engines, this is exactly the place where a query that "just worked" starts failing.
Version and NLS settings can shift the symptoms. The conversion of strings like '1,5' depends on NLS_NUMERIC_CHARACTERS, since in some locales the decimal separator is a comma. That means a value that converts cleanly in one session can fail in another with different NLS settings. If the error appears only for certain users or connections, check NLS parameters before blaming the data.
Fix it in application code, not just the database
If the failing statement came from an application, patching the query is a temporary measure. The durable fix is at the boundary.
Validate and type parameters before they reach the database. Bind variables are the usual vector for this error when a web form passes a string into a numeric column comparison. Parse the input to a number in your application layer, and reject or normalize non-numeric input with a clear error message before it becomes an ORA-01722. A parameterized query doesn't prevent this—it prevents injection but not type mismatch.
Match the parameter type to the column type. If the driver lets you declare the parameter as a NUMBER for a numeric column, do that. The conversion then happens in the driver, where you can catch it, rather than in the SQL engine.
Handle the empty and null cases explicitly. Applications often send empty strings for optional numeric fields. In Oracle, '' is NULL, so it may pass silently; other engines differ. Decide what an empty numeric field means and normalize it (to NULL, or to a default) before binding.
Add a regression test with the bad value. The value that caused the failure—'N/A', '1,234', a blank space—should become a test case. Without it, the same bug returns with the next data import.
For the query-side guard when you genuinely can't fix the source, the standard pattern is a CASE expression that returns the converted number only for validated input and a sentinel otherwise, then filters on the sentinel. The exact syntax differs by engine, and the validation expression (regular expression, character check, or a helper function) must match your acceptable formats, including a sign and a single decimal separator. Test the validation expression against your real data before trusting it; a regex that rejects -5.5 will silently drop legitimate rows.
A stop condition: if the "non-numeric" value turns out to be legitimate business data—for example, a product code that looks numeric but isn't—the column should probably be VARCHAR2 everywhere, and the numeric comparison is the design flaw. Don't force a conversion; fix the model. If you can't change the schema because of legacy constraints, involve the schema owner or DBA before applying query-level workarounds at scale.
Verify the fix and prevent the next one
After applying a change, confirm three things: the failing statement runs, the results are correct, and the condition can't silently recur.
Run the exact original reproduction. Same statement, same bind values. It should succeed. Then run the statement with a broad data sample, not just the one row you fixed, because other bad rows may be lurking.
Check the results, not just the absence of an error. A conversion fix that changes semantics (like the string-comparison trap above) can make the query run while returning wrong answers. Compare before-and-after row counts and a sample of values.
Re-scan for other conversion sites. The same bad column often appears in multiple queries. Search your codebase for references to the column and audit each comparison.
Add prevention at the right layer. A CHECK constraint, a validated input path, or a data-quality test in your pipeline catches the next bad value before it reaches production. For Oracle, a constraint like "this column contains only numeric characters" on a text column is enforceable with a validation expression.
The honest nuance: no single prevention step covers every case. A CHECK constraint on the column handles future inserts; it doesn't clean existing rows, and it can't apply to columns you don't control. Application validation handles the input path but not direct loader jobs. The layered approach—constraint plus input validation plus a data-quality check—is what actually keeps ORA-01722 from returning.
Quick FAQ
Why did ORA-01722 suddenly appear in code that worked for years? Almost always a new row, a new parameter, or a locale/NLS change. The query didn't change; the data it encountered did. Reproducing with the specific input is the fastest route.
Can a bind variable cause ORA-01722? Yes. If a string is bound where a numeric column comparison expects a number, Oracle converts the string per row, and one non-numeric value fails the statement. Check the actual bound values in the log.
Does casting to a string instead of a number fix it safely? Sometimes, but it changes comparison semantics—'9' > '10' is true as text—so it can hide bugs. Use it only when you understand and accept the resulting ordering and matching rules.
Is this the same as ORA-06502? No. ORA-01722 is a failed string-to-number conversion, typically from implicit conversion or TO_NUMBER on bad input. ORA-06502 is a numeric or value error in PL/SQL, often from a variable too small for the assigned value. Read the actual message before fixing.