ORA-06502: PL/SQL numeric or value error Explained and Fixed

Common SQL Errors

Galaxy Team
June 25, 2025
Runtime Error

ORA-06502 occurs when a PL/SQL variable cannot hold or convert the assigned value due to size, precision, or datatype mismatch.

Oracle
Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

How to resolve ORA-06502: PL/SQL: numeric or value error?

ORA-06502: PL/SQL numeric or value error appears when a value overflows or mismatches a variable’s datatype or size. Enlarge the variable definition or add explicit TO_CHAR/TO_NUMBER conversions at the failing line to fix the error.

Typical Error Message

ORA-06502: PL/SQL: numeric or value error

Explanation

Table of Contents

How do I resolve ORA-06502: PL/SQL: numeric or value error?

ORA-06502 is a runtime exception raised by Oracle PL/SQL when the engine cannot fit a value into a variable’s declared type or size. The message appears immediately after the offending statement, stops execution, and rolls back uncommitted work.

The error can surface inside anonymous blocks, stored procedures, triggers, or packages and is version-agnostic—from Oracle 9i through 23c.

Correcting it is important because repeated failures can hide data corruption, break application flows, and waste compute resources.

What Causes This Error?

Datatype mismatch tops the list: assigning a VARCHAR2 literal into a NUMBER, or casting a string like 'ABC' to NUMBER triggers ORA-06502.

String overflow occurs when the data length exceeds the target variable’s declared size, e.g., VARCHAR2(10) receiving 20 characters.

Numeric overflow happens when the value’s precision or scale is larger than the variable’s definition, such as NUMBER(5) receiving 123456.

Wrong parameter modes or lengths in DBMS_OUTPUT or UTL_FILE can also raise the exception.

PL/SQL collections throw ORA-06502 when indices are out of range or element assignments break datatype rules.

How to Fix ORA-06502

Identify the statement: enable SERVEROUTPUT or examine stack trace to pinpoint the line number.

Compare source and target datatypes; adjust either the variable definition or the expression with TO_CHAR or TO_NUMBER conversions.

Increase string or number size: change VARCHAR2(10) to VARCHAR2(100) or NUMBER(5) to NUMBER(10).

Use EXCEPTION handling to trap and log the error, but always correct the root size or datatype issue before re-raising.

Common Scenarios and Solutions

Implicit conversion failure: pass a CHAR parameter containing letters to a NUMBER column.

Solution: sanitize input or call TO_NUMBER with default ON CONVERSION ERROR handling.

Bulk collect into collection of NUMBER while query selects VARCHAR2 column. Solution: match collection element type to query column.

DBMS_OUTPUT.PUT_LINE buffer overflow with long strings.

Solution: split long messages or call PUT_LINE in loops.

Best Practices to Avoid This Error

Declare variables with generous sizes and consistent datatypes mirroring table columns.

Validate and cast external inputs at API boundaries using explicit conversion functions with EXCEPTION clauses.

Enable compile-time warnings (PLSQL_WARNINGS) to catch size mismatches early.

Adopt unit tests that populate boundary values to surface hidden overflow conditions.

Galaxy’s AI Copilot highlights datatype conflicts as you type, reducing runtime surprises.

Related Errors and Solutions

ORA-01403: no data found – arises when SELECT returns nothing into scalar variable; solve with EXISTS checks.

ORA-06512: at line X – not an error itself but a stack trace line accompanying ORA-06502; read it to locate code path.

ORA-06530: reference to uninitialized collection – fix by calling collection_name.EXTEND or assigning constructor.

Common Causes

Datatype Mismatch

Assigning a string to a numeric variable, or vice versa, forces Oracle to attempt an implicit cast.

When the cast fails, ORA-06502 is raised.

String Length Overflow

Input data longer than the target VARCHAR2, CHAR, or RAW size overflows the buffer and triggers the exception.

Numeric Precision Overflow

Values whose precision or scale exceed the NUMBER definition cannot be stored, causing a numeric error.

Invalid Collection Operation

Assigning to an index that has not been allocated or using mismatched element types in collections produces ORA-06502.

DBMS_OUTPUT or UTL_FILE Limits

Writing lines longer than 32,767 bytes or passing incorrect datatypes to these utility packages raises the error.

Related Errors

FAQs

Is ORA-06502 the same as ORA-01722?

No. ORA-06502 is a PL/SQL runtime exception, whereas ORA-01722 occurs in pure SQL during implicit conversion.

Does increasing VARCHAR2 size always solve the error?

Only if the root cause is string overflow. Datatype mismatches or numeric overflow need explicit conversions or larger NUMBER precision.

How can I find the exact line causing ORA-06502?

Run the block, read the accompanying ORA-06512 stack trace, or enable DBMS_TRACE to capture a detailed execution path.

Can Galaxy prevent ORA-06502?

Galaxy’s AI Copilot flags potential datatype and length issues while you type, helping you fix them before runtime.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo