Are you getting an ORA-06502 error message when working with Oracle SQL? Learn how to resolve it and what causes it in this article.
ORA-06502 Cause
The cause of the “ORA-06502 PL/SQL numeric or value error” can be one of many things:
- A value is being assigned to a numeric variable, but the value is larger than what the variable can handle.
- A non-numeric value is being assigned to a numeric variable.
- A value of NULL is being assigned to a variable which has a NOT NULL constraint.
Let’s take a look at the solutions for each of these causes.
The solution for this error will depend on the cause.
Let’s see an example of each of the three causes mentioned above.
Solution 1: Value Larger than Variable (Number Precision Too Large)
In this example, we have some code that is setting a numeric variable to a value which is larger than what can be stored.
Let’s create this procedure which declares and then sets a variable:
CREATE OR REPLACE PROCEDURE TestLargeNumber
AS
testNumber NUMBER(3);
BEGIN
testNumber := 4321;
END;
If we compile it, it compiles with no errors.
Procedure TESTLARGENUMBER compiled
Now, let’s run the procedure.
EXEC TestLargeNumber;
We get an error:
Error starting at line : 8 in command - EXEC TestLargeNumber Error report - ORA-06502: PL/SQL: numeric or value error: number precision too large ORA-06512: at "SYSTEM.TESTLARGENUMBER", line 5 ORA-06512: at line 1 06502. 00000 - "PL/SQL: numeric or value error%s" *Cause: An arithmetic, numeric, string, conversion, or constraint error occurred. For example, this error occurs if an attempt is made to assign the value NULL to a variable declared NOT NULL, or if an attempt is made to assign an integer larger than 99 to a variable declared NUMBER(2). *Action: Change the data, how it is manipulated, or how it is declared so that values do not violate constraints.
The error we’ve gotten is “ORA-06502: PL/SQL: numeric or value error: number precision too large”. It also includes an ORA-06512, but that error just mentions the next line the code is run from, as explained in this article on ORA-06512.
This is because our variable testNumber can only hold 3 digits, because it was declared as a NUMBER(3). But, the value we’re setting it to a few lines later is 4 digit long (4321).
So, the value is too large for the variable.
To resolve it, increase the size of your variable, or manipulate your value to fit the size of the variable (if possible).
In our example , we can change the size of the variable.
CREATE OR REPLACE PROCEDURE TestLargeNumber
AS
testNumber NUMBER(4);
BEGIN
testNumber := 4321;
END;
Procedure TESTLARGENUMBER compiled
Now, let’s run the procedure.
EXEC TestLargeNumber;
PL/SQL procedure successfully completed.
The procedure runs successfully. We don’t get any output (because we didn’t code any in), but there are no errors.
Read more on the Oracle data types here.
While you’re here, if you want an easy-to-use list of the main features in Oracle SQL, get my SQL Cheat Sheet here:
Solution 2: Non-Numeric Value
Another way to find and resolve this error is by ensuring you’re not setting a numeric variable to a non-numeric value.
For example, take a look at this function.
CREATE OR REPLACE PROCEDURE TestNonNumeric
AS
testNumber NUMBER(4);
BEGIN
testNumber := 'Yes';
END;
Procedure TESTNONNUMERIC compiled
The procedure compiles successfully. Now, let’s fun the function.
EXEC TestNonNumeric;
Error starting at line : 8 in command - EXEC TestNonNumeric Error report - ORA-06502: PL/SQL: numeric or value error: character to number conversion error ORA-06512: at "SYSTEM.TESTNONNUMERIC", line 5 ORA-06512: at line 1 06502. 00000 - "PL/SQL: numeric or value error%s" *Cause: An arithmetic, numeric, string, conversion, or constraint error occurred. For example, this error occurs if an attempt is made to assign the value NULL to a variable declared NOT NULL, or if an attempt is made to assign an integer larger than 99 to a variable declared NUMBER(2). *Action: Change the data, how it is manipulated, or how it is declared so that values do not violate constraints.
The error we get is “ORA-06502: PL/SQL: numeric or value error: character to number conversion error”.
This happens because our variable testNumber is set to a NUMBER, but a few lines later, we’re setting it to a string value which cannot be converted to a number
To resolve this error:
- Ensure the value coming in is a number and not a string.
- Convert your string to a number using TO_NUMBER (the conversion might happen implicitly but this may help).
- Convert your string to the ASCII code that represents the string using the ASCII function.
- Change the data type of your variable (but check that your code is getting the right value first).
The solution you use will depend on your requirements.
Solution 3: NOT NULL Variable
This error can appear if you try to set a NULL value to a NOT NULL variable.
Let’s take a look at this code here:
CREATE OR REPLACE PROCEDURE TestNonNull
AS
testNumber NUMBER(4) NOT NULL := 10;
nullValue NUMBER(4) := NULL;
BEGIN
testNumber := nullValue;
END;
Procedure TESTNONNULL compiled
Now, the reason we’re using a variable to store NULL and not just setting testNumber to NULL is because we get a different error in that case. Besides, it’s probably more likely that your NULL value will come from another system or a database table, rather than a hard-coded NULL value.
Let’s run this function now.
Error starting at line : 9 in command - EXEC TestNonNull Error report - ORA-06502: PL/SQL: numeric or value error ORA-06512: at "SYSTEM.TESTNONNULL", line 6 ORA-06512: at line 1 06502. 00000 - "PL/SQL: numeric or value error%s" *Cause: An arithmetic, numeric, string, conversion, or constraint error occurred. For example, this error occurs if an attempt is made to assign the value NULL to a variable declared NOT NULL, or if an attempt is made to assign an integer larger than 99 to a variable declared NUMBER(2). *Action: Change the data, how it is manipulated, or how it is declared so that values do not violate constraints.
We get the ORA-06502 error.
This error message doesn’t give us much more information. But, we can look at the code on line 6, as indicated by the message. We can see we have a variable that has a NOT NULL constraint, and the variable is NULL.
To be sure, we can output some text in our demo when it is null.
CREATE OR REPLACE PROCEDURE TestNonNull
AS
testNumber NUMBER(4) NOT NULL := 10;
nullValue NUMBER(4) := NULL;
BEGIN
IF (nullValue IS NULL) THEN
dbms_output.put_line('Value is null!');
ELSE
testNumber := nullValue;
END IF;
END;
Now let’s call the procedure.
EXEC TestNonNull;
Value is null!
The output shows the text message, indicating the value is null.
ORA-06502 character string buffer too small
This version of the error can occur if you set a character variable to a value larger than what it can hold.
When you declare character variables (CHAR, VARCHAR2, for example), you need to specify the maximum size of the value. If a value is assigned to this variable which is larger than that size, then this error will occur.
For example:
DECLARE
charValue VARCHAR2(5);
BEGIN
charValue := 'ABCDEF';
END;
If I compile this code, I get an error:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small ORA-06512: at line 4
This happens because the variable is 5 characters long, and I’m setting it to a value which is 6 characters long.
You could also get this error when using CHAR data types.
DECLARE
charValue CHAR(5);
BEGIN
charValue := 'A';
charValue := charValue || 'B';
END;
ORA-06502: PL/SQL: numeric or value error: character string buffer too small ORA-06512: at line 5
This error happens because the CHAR data type uses the maximum number of characters. It has stored the value of A and added 4 space characters, up until its maximum value of 5.
When you try to concatenate a value of B to it, the resulting value is ‘A B’, which is 6 characters.
To resolve this, use a VARCHAR2 variable instead of a CHAR, and ensure the maximum size is enough for you.
ORA-06502: pl/sql: numeric or value error: null index table key value
Sometimes you might get this error message with the ORA-06502 error:
ORA-06502: pl/sql: numeric or value error: null index table key value
This means that either:
- Your index variable is not getting initialized, or
- Your index variable is getting set to NULL somewhere in the code.
Check your code to see that neither of these two situations are happening.
ORA-06502: pl/sql: numeric or value error: bulk bind: truncated bind
You might also get this specific error message:
ORA-06502: pl/sql: numeric or value error: bulk bind: truncated bind
This is caused by an attempt to SELECT, UPDATE, or INSERT data into a table using a PL/SQL type where a column does not have the same scale as the column in the table.
For example, you may have declared a variable in PL/SQL to be VARCHAR2(100), but your table is only a VARCHAR2(50) field. You may get this error then.
You may also get this error because some data types in PL/SQL have different lengths in SQL.
To resolve this, declare your variables as the same type as the SQL table:
type t_yourcol is table of yourtable.yourcol%TYPE;
So, that’s how you resolve the ORA-06502 error.
While you’re here, if you want an easy-to-use list of the main features in Oracle SQL, get my SQL Cheat Sheet here:
Нельзя присвоить значение NULL
переменной объявленной с ограничением NOT NULL
.
06502, 00000, «PL/SQL: numeric or value error%s»
*Cause: An arithmetic, numeric, string, conversion, or constraint error
occurred. For example, this error occurs if an attempt is made to
assign the value NULL to a variable declared NOT NULL, or if an
attempt is made to assign an integer larger than 99 to a variable
declared NUMBER(2).
Здесь:
a varchar2(10) not null := '',
пустая строка интерпретируется как NULL
. Тут подробнее, почему.
v_recx(1).a := 'BBB';
Эта строка будет скомпилирована, так как компилятор не проверяет присваиваемых значений во время компиляции. При выполнении блока, ещё до присваивания полю значения 'BBB'
, поля записи будут инициализированы, где и произойдёт попытка присвоить полю a
с ограничением NOT NULL
значения NULL
.
I have the below pl/sql procedure
PROCEDURE insert_p(
p_batch_rec IN ra_batches%rowtype,
p_batch_id OUT NOCOPY ra_batches.batch_id%type,
p_name OUT NOCOPY ra_batches.name%type
)
batch_id is NUMBER(18,0) and p_name is VARCHAR2(50 CHAR)
I’m calling the procedure with
insert_p (l_batch_rec, p_batch_id, p_name);
where p_batch_id:=NULL and p_name:=NULL
I get the conversion error only on the first time I run the procedure. If I run again without changes, it runs fine. To reproduce the error, I disconnect and connect again.
Any ideas why does this error come and how should I resolve the same???
ORA-06502: PL/SQL: numeric or value error: character to number conversion error occurs when a character value is assigned to a numeric variable in the oracle PL/SQL code. When a non-numeric value is assigned to a numeric datatype variable, the character cannot be converted to a number. A numeric data type variable cannot be assigned a character value. If you try to assign character to number, the conversion error ORA-06502: PL/SQL: numeric or value error: character to number conversion error will be thrown.
The number conversion error happens when the character value is assigned to a number variable. It is not possible to convert the non-numeric character value to a numeric number. The data type of the variable should be changed to character or a numeric value should be assigned. The assigned value and the declared datatype must be same to store the value. Otherwise, the character to number conversion error ORA-06502: PL/SQL: numeric or value error: character to number conversion error would be shown.
Exception
If you run the above code in Oracle, you will get the stack trace error shown below. The numeric data type is attempted to be assigned to the character value.
declare
empid numeric(4);
begin
empid := 'A101';
end;
Error report -
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 4
06502. 00000 - "PL/SQL: numeric or value error%s"
Problem
A character employee id is assigned to variable empid in the example below. The empid’s data type is a number with a size of four. The oracle error is thrown if the character value is assigned to the number data type variable.
declare
empid numeric(4);
begin
empid := 'A101';
end;
Output
declare
empid numeric(4);
begin
empid := 'A101';
end;
Error report -
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 4
06502. 00000 - "PL/SQL: numeric or value error%s"
Cause
An arithmetic, numeric, string, conversion, or constraint error occurred. For example, this error occurs if an attempt is made to assign the value NULL to a variable declared NOT NULL, or if an attempt is made to assign an integer larger than 99 to a variable declared NUMBER(2).
Action
Change the data, how it is manipulated, or how it is declared so that values do not violate constraints.
Solution 1
The data type of the variable should be changed to character data type. The character data type will store both the alphabet and the value of a number. The character data type is shown in the example below.
declare
empid varchar(4);
begin
empid := 'A101';
end;
Output
PL/SQL procedure successfully completed.
Solution 2
A number should be used as the value. Make sure the origin of the value. This variable receives the value incorrectly. If the code bug exists and is fixed, the value assigned to the variable would be a number value. The numeric value will be stored in the number data type variable.
declare
empid numeric(4);
begin
empid := 101;
end;
Output
PL/SQL procedure successfully completed.
Solution 3
Handling the exception from PL/SQL code is another method for dealing with this error. If an error occurs, handle it and take a different action for the value.
declare
empid numeric(4);
begin
empid := 'A101';
exception
WHEN OTHERS THEN
empid :=0;
end;
Output
PL/SQL procedure successfully completed.
ORA-06502 means that PL/SQL engine cannot convert a character-typed string into a number or a subset of arithmetic for overall evaluation. Mostly, it’s because of the following problems:
- Numeric Type Conversion
- Numeric Operator Precedence
A. Numeric Type Conversion
ORA-06502 tells you that PL/SQL engine cannot convert a string into a number. Which means, an arithmetic, numeric, string, conversion, or constraint error occurred. Let’s see a normal case first.
SQL> set serveroutput on;
SQL> declare
2 v_num number;
3 begin
4 v_num := 123;
5 dbms_output.put_line('The number is ' || v_num);
6 end;
7 /
The number is 123
PL/SQL procedure successfully completed.
A number 123 is assigned to variable V_NUM which accept only NUMBER type. So there’s no conversion needed. But what if we assign a string to the variable?
SQL> declare
2 v_num number;
3 begin
4 v_num := '123';
5 dbms_output.put_line('The number is ' || v_num);
6 end;
7 /
The number is 123
PL/SQL procedure successfully completed.
As you can see, PL/SQL engine converted the string into a number, then assigned it into the variable.
Now, let’s try some basic arithmetic expressions.
SQL> declare
2 v_num number;
3 begin
4 v_num := 2 + 2;
5 dbms_output.put_line('The number is ' || v_num);
6 end;
7 /
The number is 4
PL/SQL procedure successfully completed.
OK, the variable accepts value, the result of evaluation, no ORA-06502. What if we use it as a string?
SQL> declare
2 v_num number;
3 begin
4 v_num := '2 + 2';
5 dbms_output.put_line('The number is ' || v_num);
6 end;
7 /
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 4
PL/SQL engine tried to convert the string into a number, but it failed with ORA-06502. This time, V_NUM cannot accept the result.
The solution to this type of error is to avoid implicit type conversion if possible.
B. Numeric Operator Precedence
To better understand ORA-06502, let’s see a more advanced topic about operator precedence in Oracle database. In the following example, we tried to output a string that concatenate an arithmetic.
SQL> begin
2 dbms_output.put_line('The number is ' || 2 + 2);
3 end;
4 /
begin
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 2
ORA-06502 was thrown eventually. Since || (concatenation) and + (addition) operators are at the same level of operator precedence, PL/SQL engine will evaluate them in the order of presence.
First, it concatenated «The number is » and «2» into «The number is 2», which was successful, but when it tried to add the last value «2», it failed to convert the former string into a number and threw ORA-06502.
Solutions
1. Rearrange the Output
We should make PL/SQL engine deal with the numeric evaluation first, then the concatenation by rearranging the output.
SQL> begin
2 dbms_output.put_line(2 + 2 || ' is the number.');
3 end;
4 /
4 is the number.
PL/SQL procedure successfully completed.
This time, the expression is good because the order of presence of operators has been changed.
2. Override Operator Precedence
Beside rearranging the order of presence, how can we make the latter take the precedence over the former to fix the problem? Here is the trick for our PL/SQL block of codes.
SQL> begin
2 dbms_output.put_line('The number is ' || (2 + 2));
3 end;
4 /
The number is 4
PL/SQL procedure successfully completed.
As you can see, we used a parenthesis to override operator precedence. The evaluation will start from the highest precedence which is 2 + 2 numeric value inside the parentheses to the rest according to their operator precedence defined in Oracle. This is how we escape from ORA-06502.
In PL/SQL, if multiple parentheses are used in your expression, the evaluation will start from the inner to the outer.
A very similar error that you might see in your statements is ORA-01722: invalid number, which is also related to conversion issues of numeric values.