Hello Tom !
It seems that Oracle has a bug in TO_NUMBER function on strings with trailing CHR(0).
For example it happens
when SUBSTR function is used for which there is not defined length parameter.
Example:
select substr(session_id, 1, instr(session_id, ',')-1) s,
dump(substr(session_id, 1, instr(session_id, ',')-1)) s_d,
substr(session_id, instr(session_id, ',') + 1) sn,
dump(substr(session_id, instr(session_id, ',') + 1)) sn_d,
rtrim(substr(session_id, instr(session_id, ',') + 1), chr(0)) asn,
dump(rtrim(substr(session_id, instr(session_id, ',') + 1), chr(0))) asn_d
from USER_SCHEDULER_JOB_RUN_DETAILS
where job_name = :some_job_name;
One record in my case looks like this:
S S_D SN SN_D ASN ASN_D
================================================================================================
3712 Typ=1 Len=4: 51,55,49,50 59803 Typ=1 Len=6: 53,57,56,48,51,0 59803 Typ=1 Len=5: 53,57,56,48,51
Although the results from SQL*Plus looks the same
equality check on
substr(session_id, instr(session_id, ',') + 1)
and
rtrim(substr(session_id, instr(session_id, ',') + 1), chr(0))
is FALSE !
And of course it should not be !
And even worse - TO_NUMBER function fails with INVALID NUMBER when applied on
string which has trailing CHR(0).
I.e. in this case TO_NUMBER(SN) raises an exception and TO_NUMBER(ASN) does not.
USER_SCHEDULER_JOB_DETAILS is just an example here.
It happens also on normal tables.
And also I've found it only when SUBSTR function is used without LENGTH parameter,
but maybe it also happens for some other string functions.
What is strange that this kind of SUBSTR does not always produce strings with trailing CHR(0).
Only in some cases.
I can't find anything similar in BUGs repository in Oracle Support.
Can you check please if there is some patch for this
and if some other string functions are affected.
Thank you !
And yes - I've found it on 11.2, 12.1 and 12.2 versions.
BR,
Hrvoje