Skip to Main Content
  • Questions
  • to_char a big number insert into database become scientific notation

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question.

Asked: April 11, 2024 - 3:16 am UTC

Last updated: April 16, 2024 - 9:57 am UTC

Version: 19.3c

Viewed 1000+ times

You Asked

Hi, Tom. Please see below script.

create table t0326 (id number, num varchar2(100));  

declare
  v_empno number:=125854437665589038536841445202964995521300;
begin
  dbms_output.put_line('v_empno -- ' || v_empno);
  dbms_output.put_line('to_char(v_empno) -- '|| to_char(v_empno));
  insert into t0326 values(10, to_char(v_empno));
  commit;
end;
/ 

v_empno -- 125854437665589038536841445202964995521300
to_char(v_empno) -- 125854437665589038536841445202964995521300


select * from t0326;

        ID NUM
---------- ------------------------------------------------------------
        10 1.2585443766558903853684144520296500E+41
      
       
declare
  v_empno number:=125854437665589038536841445202964995521300;
  v_s_empno varchar2(100);
begin
  v_s_empno := to_char(v_empno);
  dbms_output.put_line('v_empno -- ' || v_empno);
  dbms_output.put_line('to_char(v_empno) -- '|| to_char(v_empno));
  dbms_output.put_line('v_s_empno -- '|| v_s_empno);
  insert into t0326 values(20, to_char(v_empno));
  insert into t0326 values(30, v_s_empno);
  insert into t0326 values(40, to_char(v_empno, 'FM999999999999999999999999999999999999999999999999999999999'));
  commit;
end;
/        

v_empno -- 125854437665589038536841445202964995521300
to_char(v_empno) -- 125854437665589038536841445202964995521300
v_s_empno -- 125854437665589038536841445202964995521300
        

select * from t0326;  

        ID NUM
---------- -----------------------------------------------------------------------
        10 1.2585443766558903853684144520296500E+41
        20 1.2585443766558903853684144520296500E+41
        30 125854437665589038536841445202964995521300
        40 125854437665589038536841445202964995521300      


It display normal when "to_char(v_empno)" in dbms_output.put_line. But insert to database convert to scientific notation.
I try two solutions to solve this problem. Please see below.
1. use a variable to store to_char(v_empno), then insert this varaible to database.
2. use to_char(xx, FMT) to control the format.
I wonder why "to_char(v_empno)" in dbms_output.put_line is not scientific notation ? why add a temp variable could solve this problem ?

and Chris said...

Different environments can have different default values for conversions etc. Here you're seeing a difference between the SQL & PL/SQL engines.

In this code block you're running pure PL/SQL:

declare
  v_empno number:=125854437665589038536841445202964995521300;
  v_s_empno varchar2(100);
begin
  v_s_empno := to_char(v_empno);
  dbms_output.put_line('v_empno -- ' || v_empno);
end;
/


Whereas with this the insert uses the SQL engine:

declare
  v_empno number:=125854437665589038536841445202964995521300;
begin
  insert into t0326 values(10, to_char(v_empno));
end;
/ 


To be certain values are formatted in a particular way use a format mask when doing data type conversions.

More to Explore

DBMS_OUTPUT

More on PL/SQL routine DBMS_OUTPUT here