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

Common SQL Errors

Runtime

Oracle raises ORA-06502 when a PL/SQL operation tries to assign, convert, or manipulate data that exceeds the target datatype’s size or precision.

Oracle Database
Sign up for the latest in common SQL errors from the Galaxy Team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

What is ORA-06502: PL/SQL: numeric or value error?

ORA-06502: PL/SQL: numeric or value error appears when a PL/SQL block overflows or mismatches a variable’s datatype or length. Fix it by correcting datatype sizes, handling NULLs, and adding explicit conversions that fit the destination variable.

Typical Error Message

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

Explanation

What is ORA-06502: PL/SQL: numeric or value error?

ORA-06502 signals that Oracle attempted to move, convert, or calculate data in PL/SQL but the result could not fit the receiving variable. The database stops execution and rolls back the anonymous block or stored program.Developers see this error in functions, procedures, triggers, or anonymous blocks whenever numeric overflow, string length overflow, or datatype mismatch occurs. Addressing it quickly prevents broken business logic and data loss.

What Causes This Error?

Datatype size overflow happens when a value exceeds a declared VARCHAR2, NUMBER (p,s), or RAW length.Implicit datatype conversion fails if Oracle cannot safely cast between VARCHAR2 and NUMBER or DATE.PL/SQL CHAR-to-VARCHAR2 assignments include trailing blanks that may overflow the target.Calling PL/SQL programs with mismatched parameter sizes or OUT parameters smaller than the returned value triggers the exception.Numeric computations that exceed NUMBER precision or divide by values producing scientific notation beyond the scale also raise ORA-06502.

How to Fix ORA-06502

Identify the exact line causing the error by rerunning the block with ALTER SESSION SET PLSQL_WARNINGS='ENABLE:INFO' and examining the stack trace.Increase variable lengths (e.g., VARCHAR2(4000)VARCHAR2(32767)) or precision (NUMBER(5,2)NUMBER(9,2)).Add explicit casts such as TO_NUMBER, TO_CHAR, or TO_DATE to ensure predictable conversions.Validate external input lengths before assignment using LENGTH or DBMS_ASSERT to avoid overflow.Wrap susceptible code in BEGIN … EXCEPTION WHEN VALUE_ERROR THEN … END; to handle the error gracefully.

Common Scenarios and Solutions

String concatenation overflow: use VARCHAR2(32767) in PL/SQL or CLOB.Numeric multiplication overflow: expand precision or apply ROUND to reduce scale.Parameter mismatch between PL/SQL program and caller: align datatype lengths in both specs.Bulk collect into smaller record fields: enlarge record or limit fetched column size via SUBSTR.

Best Practices to Avoid This Error

Declare variables with generous lengths unless memory is critical; PL/SQL allocates only used space.Validate all external inputs; reject unexpected lengths early.Use %TYPE and %ROWTYPE anchors to inherit column precision automatically.Enable PLSQL_CODE_TYPE=NATIVE and PLSQL_OPTIMIZE_LEVEL=3 for better compile-time checks.Automate unit tests in Galaxy’s workspace to catch datatype issues before production deployment.

Related Errors and Solutions

ORA-01438: value larger than specified precision — occurs at SQL layer; enlarge column precision.ORA-06512: at line nn — accompanies ORA-06502 stack; pinpoints code line.ORA-01858: a non-numeric character found where a numeric is expected — fix by validating date strings.ORA-00932: inconsistent datatypes — ensure compatible datatypes during joins or unions.

Common Causes

Assigning a 5-digit value to a variable declared NUMBER(4).

Concatenating strings that exceed VARCHAR2 limit.

Receiving external JSON or API input longer than expected.

Mismatching IN/OUT parameter sizes between procedure spec and body.

Implicit VARCHAR2-to-NUMBER conversion of alphabetic characters.

Related Errors

ORA-01438 (precision overflow) – occurs in SQL layer when inserting.ORA-06512 – stack trace line indicator accompanying runtime errors.ORA-01858 (non-numeric character in date) – arises during date conversion.ORA-00932 (inconsistent datatypes) – raised for mismatched SQL datatypes.

FAQs

Why does ORA-06502 show line number 1 even in large programs?

The runtime engine reports the first PL/SQL line of the anonymous block. Compile the code into a stored procedure to get an accurate stack trace via ORA-06512.

Can I ignore ORA-06502 with PRAGMA EXCEPTION_INIT?

Yes, declare PRAGMA EXCEPTION_INIT(e_valerr,-6502); and then catch e_valerr, but handle root causes instead of silencing them.

Does increasing VARCHAR2 length hurt performance?

No, PL/SQL allocates memory dynamically. Larger limits rarely impact speed unless heavily repeated in tight loops.

How does Galaxy help prevent ORA-06502?

Galaxy’s AI Copilot inspects variable declarations, flags potential overflows, and auto-suggests matching %TYPE anchors, reducing human error.

Check out some other errors

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