How to find out the number of digits of an oracle number

23,360

How about....

SELECT LENGTH(TRANSLATE(TO_CHAR(3.0123456789),'1234567890.-','1234567890')) 
FROM dual

The translate simply removes the non numeric characters .-

Share:
23,360
Ioan Paul Pirau
Author by

Ioan Paul Pirau

// C++: 6 yrs+ ' Vb.Net: 2 yrs

Updated on August 26, 2020

Comments

  • Ioan Paul Pirau
    Ioan Paul Pirau over 3 years

    I have a table in oracle that looks like this:

    name              |     type    | nullable  
    ------------------------------------------
    person_name       | varchar(20) | yes  
    weight_coeficient | number      | yes
    ...
    

    How can I figure out how many digits a value of weight_coeficient has ? For example:

    3.0123456789 has 11 digits (precision = 11) and 10 digits after the decimal (scale = 10)

    Is there any sql command/function that does that, something like GetPrecision( select.. ) that returns 11 ?

    Note also that the definition of the table does not specify scale and precision. So as far as I know the maximum precision is applied for all the numbers. So I'm not interested in finding out the precision (= 48) of the definition, but the precision of a specific value in the table. Is that possible just using oracle commands ?

    Thank you in advance,
    JP

  • Ioan Paul Pirau
    Ioan Paul Pirau over 12 years
    Thank you for the answer. But 3.0123456789 was an example. How can I adapt the code you wrote to use weight_coeficient instead considering that the table name is Coeficients? I would expect something like SELECT LENGTH(TRANSLATE(Select weight_coeficient from Coeficients where name = 'John Paul'),'1234567890.-','1234567890')) FROM dual or something. But this doesn't work. Sorry.. I'm not too good at sql sintax
  • Allan
    Allan over 12 years
    @John Paul: You'd be much better off with SELECT LENGTH(TRANSLATE(weight_coeficient),'1234567890.-','12345678‌​90')) from Coeficients where name = 'John Paul';
  • SiliconBadger
    SiliconBadger about 6 years
    @Allan: good tip, but you have an extra parenthesis. I think it should be SELECT LENGTH(TRANSLATE(weight_coeficient,'1234567890.-','123456789‌​0')) from...