Skip to content
Advertisement

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

DECLARE
    a NUMBER;
    b NUMBER;
BEGIN
    a :=: a;
    b :=: b;
    DBMS_OUTPUT.PUT_LINE('sum = '|| a+b);
END;

I am getting error as numeric or value error

Advertisement

Answer

The problem is with operator precedence:

Oracle evaluates operators with equal precedence from left to right within an expression.

… and as the table shows + and || have equal precedence. So in this statement:

DBMS_OUTPUT.PUT_LINE('sum = '|| a+b);

this is interpreted as 'sum = '|| a which gives you a string, then <result string> + b, and string + number will cause the error you see if the string cannot be implicitly converted to a number – and 'sum = 1' can’t be.

You can add parentheses to override the default precedence:

DBMS_OUTPUT.PUT_LINE('sum = '|| (a+b));

db<>fiddle

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement