ORA-01722: Invalid Number

Introduction
Causes of ORA-01722
Examples of ORA-01722
Implicit Data Conversion
Solutions to ORA-01722
Conclusion

Introduction

The ORA-01722: Invalid Number error is a common issue in Oracle databases that occurs when a SQL statement attempts to convert a non-numeric string into a number. This error can arise during data type conversion operations, especially when numeric data types such as INTEGER, NUMBER, or FLOAT are involved.

Causes of ORA-01722

The ORA-01722: Invalid Number error may be caused by:

  • Incorrect Data Format: When a SQL statement expects a numeric value but encounters a non-numeric string. This often happens during arithmetic operations or comparisons.
  • Data Type Mismatch: When comparing or assigning values between different data types without proper conversion.
  • Implicit Data Conversion: Oracle attempts to implicitly convert data types based on context, and the conversion fails due to incompatible formats.

Examples of ORA-01722

Example 1: Performing arithmetic operations on non-numeric data:


SQL> SELECT 'ABC' + 10 FROM dual;
SELECT 'ABC' + 10 FROM dual
       *
ERROR at line 1:
ORA-01722: invalid number
        

Example 2: Using a non-numeric string in a numeric context:


SQL> SELECT * FROM employees WHERE employee_id = 'XYZ';
SELECT * FROM employees WHERE employee_id = 'XYZ'
                                            *
ERROR at line 1:
ORA-01722: invalid number
        

Implicit Data Conversion

Implicit data conversion in Oracle occurs when Oracle automatically converts data from one type to another. While this can be convenient, it can also lead to ORA-01722 errors if the conversion fails.
For example:


SQL>SELECT * FROM orders WHERE order_date = '2024-07-01';
SELECT * FROM orders WHERE order_date = '2024-07-01';
                                            *
ERROR at line 1:
ORA-01722: invalid number
        

In the above query, '2024-07-01' is a string, but order_date is a DATE column. Oracle attempts to convert '2024-07-01' to a DATE type, which may fail if the string format is incorrect.

Solutions to ORA-01722

To resolve ORA-01722: Invalid Number, consider the following solutions:

  • Verify Data: Double-check the data being inserted or queried to ensure it matches the expected data type.
  • Use TO_NUMBER Function: Explicitly convert non-numeric strings to numbers using the TO_NUMBER function.

SQL> SELECT TO_NUMBER('123') FROM dual;

TO_NUMBER('123')
----------------
             123
            
  • Check Data Integrity: Review data integrity constraints and ensure that only valid numeric values are inserted or updated.
  • Use Proper Data Types: Define columns with appropriate data types to avoid implicit conversions that might fail.
  • Handle Null Values: Consider using the NVL or NVL2 functions to handle NULL values gracefully during calculations.

SELECT NVL(TO_NUMBER(salary), 0) FROM employees;
            

Conclusion

ORA-01722: Invalid Number is a SQL error in Oracle databases that occurs due to incorrect or incompatible data type conversions. By understanding the causes, reviewing examples, and applying appropriate solutions such as explicit data type conversions and data validation, you can effectively resolve this error and ensure smooth SQL operations.



Related content



Rate Your Experience

: 89 : 1


Last updated in July, 2024

Online Tests
Read more

Oracle Database
Read more

MSSQL Database
Read more

PostGres Database
Read more

Linux
Read more

ASP/C#
Read more

Quick Access