Skip to Main Content
  • Questions
  • Number filtering from Oracle with Decimal

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Presanth.

Asked: July 01, 2016 - 4:27 am UTC

Last updated: July 02, 2016 - 1:24 am UTC

Version: 10g

Viewed 1000+ times

You Asked

Hi Team,
We had a situation where the customer is pulling a report from a table. This table has a number format as number (5,2) . They have a value 9 and also its updating another value as 9.00 while selecting it from the table if we use to_char(n,999.99) then for both 9 and 9.00 it gives the same 9.00 , What they looking is they want to retrieve the value as it is inserted in Oracle.I think Oracle will consider 9 and 9.00 as same , is there a way we can get this output for the value as same as we inserted? May be some thing to do in the coding but would be great i can get the sample for this

Thanks
Pksadoor

and Connor said...

Internally they are identical, so once you've inserted them, you'll not be able to distinguish between the two.

SQL> create table t ( x number(5,2));

Table created.

SQL>
SQL> insert into t values (9);

1 row created.

SQL> insert into t values (9.00);

1 row created.

SQL>
SQL> select dump(x) from t;

DUMP(X)
------------------------------------------------
Typ=2 Len=2: 193,10
Typ=2 Len=2: 193,10

2 rows selected.


If you really needed to keep the value as 'presented' to the database, you could keep it as a string, and use either an additional or virtual column for the numeric...but does that seem a lot of work to me.

SQL> create table t ( user_input varchar2(10), x number(5,2) generated always as ( to_number(user_input)));

Table created.

SQL>
SQL> insert into t (user_input) values ('9');

1 row created.

SQL> insert into t (user_input) values ('9.00');

1 row created.

SQL>
SQL> select *  from t;

USER_INPUT          X
---------- ----------
9                   9
9.00                9

2 rows selected.


Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Number filtering from Oracle with Decimal

Presanth Krishnan, July 01, 2016 - 6:42 am UTC

Thank you Let me try that

Don't expect too much

AndyP, July 01, 2016 - 8:00 am UTC

Just be careful, if adopting this approach, of assuming (as I did when virtual columns first appeared) that this would be a neat way to enforce numeric on insert into a varchar2 column. It kind of does, but probably not in a useful way:

create table t (user_input varchar2(20), x number(5,2) generated always as (to_number(user_input)));
insert into t (user_input) values ('9');
1 row created.
insert into t (user_input) values ('9.00');
1 row created.
select * from t;
USER_INPUT                    X
-------------------- ----------
9                             9
9.00                          9

insert into t (user_input) values ('non-numeric');
1 row created.

commit;
Commit complete.

select * from t;
ERROR:
ORA-01722: invalid number
no rows selected


Tom Style

Rajeshwaran, Jeyabal, July 01, 2016 - 12:39 pm UTC

Well, if this was Tom Kyte - He would have put two famous question, than providing this answer.

1) Why? why do you need 9 and 9.00 to appear to be different?

since you request for to_char(n,999.99) explicitly, even 9 would appear as 9.00, but that is correct, since you requested it explicitly.

2) It depends.
Connor McDonald
July 02, 2016 - 1:24 am UTC

Hence my emphasis:

If you *really* needed ... but does that seem a lot of work to me.