Skip to Main Content
  • Questions
  • Truncating digits before decimal in a decimal number

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Vishesh.

Asked: September 29, 2020 - 9:12 am UTC

Last updated: September 30, 2020 - 2:24 am UTC

Version: Oracle 12 C

Viewed 1000+ times

You Asked

Hi,
I am facing a problem in one of my update queries.
There is a column I am updating whose datatype is NUMBER(9,5). So it can hold 5 places after decimal, and 4 before decimal.

I am using a round function, so that takes care of places after decimal.
But for some of the records the value is getting computed as say 123546.12345, and so i am getting -1438 overflow error while updating.

I want a quick way (or function) to just get rid of the extra digits before decimal point in such cases.

So, in the example above, I want the output as 1235.12345
Please suggest a suitable way. Thanks!

and Chris said...

You can't just chop off the units or tens like that! It gives a completely different number and is nothing like rounding!

Seriously, NEVER DO THIS!

If you have a number ( 9, 5 ), the maximum value you can store is 9999.99999.

If you need to store larger numbers than this, you need to increase the precision of the column. Unless there's some reason to limit the maximum value, you may as well allow up to the maximum precision (38 digits):

create table t (
  c1 number ( 9, 5 )
);

alter table t 
  modify c1 number ( 38, 5 );



Rating

  (2 ratings)

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

Comments

Chuck Jolley, September 29, 2020 - 4:49 pm UTC

Maybe they are something, like part numbers, that aren't really numbers.
Chris Saxon
September 29, 2020 - 5:40 pm UTC

Maaaaaybe..., but they are using a numeric data type to hold the value and discuss rounding the fractional digits

Either way, removing digits from the middle of the value is a flawed idea.

Chuck Jolley, September 29, 2020 - 6:36 pm UTC

But "part number" has the word number right in it.
What could possibly go wrong?
Connor McDonald
September 30, 2020 - 2:24 am UTC

Let me count the ways....oh :-)

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.