Skip to Main Content
  • Questions
  • DATA TYPE CHANGING FROM NUMBER(8,5) TO NUMBER IN VIEW

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, shiva.

Asked: September 17, 2008 - 1:46 pm UTC

Last updated: April 12, 2011 - 1:22 pm UTC

Version: 10.2.0

Viewed 1000+ times

You Asked

Hi All,

I have a column DISC_PCT which has number(8,5) as data type in the table. when I create a view on this column as

create or replace view hdr_view
as
select round(disc_pct/100,5) from table;
end;

I now see this column as number data type in the view rather than number(8,5). Can some body suggest me if there is any way to overcome this. I appreciate your help in advance. Thanks!

and Tom said...

well, round will return a number - not a number(8,5), just a number.


You can use CAST to coerce it to a specific type:

ops$tkyte%ORA10GR2> create table t ( x number(8,5) );

Table created.

ops$tkyte%ORA10GR2>
ops$tkyte%ORA10GR2> create or replace view v
  2  as
  3  select x, round(x/100,5) x_round, cast( round(x/100,5) as number(8,5) ) x_cast
  4    from t
  5  /

View created.

ops$tkyte%ORA10GR2>
ops$tkyte%ORA10GR2> desc v
 Name                                     Null?    Type
 ---------------------------------------- -------- ----------------------------
 X                                                 NUMBER(8,5)
 X_ROUND                                           NUMBER
 X_CAST                                            NUMBER(8,5)


Rating

  (2 ratings)

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

Comments

EXCELLENT

A reader, September 18, 2008 - 12:05 am UTC

Thank you Very much Kyte..

Generalizing the question

Joachim Durchholz, April 07, 2011 - 5:52 am UTC

I'm having the same problem, but for columns that are run through a COALESCE call like this:

coalesce (FSWGRP1.FWALPH, FSWGRP2.FWALPH, FSWGRP3.FWALPH, FSWGRP4.FWALPH, FSWGRP5.FWALPH)

This is from a VIEW definition that has FSWGRP1...4 as aliases for the FSWGRP table (multiple joins with different ON conditions).
FSWGRP.FWALPH is NUMBER(3,2), but the resultant column is just NUMBER.

Do all Oracle functions lose the precision/scale information?
Maybe all expressions, even? (That would even make sense in general, though it's sort of tough in this particular use case. Particularly since the code that generates the CREATE VIEW statement isn't supposed to know about representational details.)
Tom Kyte
April 12, 2011 - 1:22 pm UTC

Sorry, but in this case, if you want the view to have a number(3,2) in it, CAST will be the necessary approach.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library