Search code examples
sqloracletrim

TRIM Trailing Zeros


I know this is a relatively simple question, but I couldn't find anything with my keywords using Google. I am referencing with a SQL (Oracle) to a column that has numbers like that:

  • 100
  • 12500
  • 300

Now I need to remove the last 2 zeros. This approach is not working:

Trim(TRAILING '00' FROM F0035.Nr)         "Sequence", 

Does anyone have any idea?

The result should be a column with numbers - not a text


Solution

  • See these two options:

    SQL> with test (col) as
      2    (select '100'   from dual union all
      3     select '12500' from dual union all
      4     select '300'   from dual
      5    )
      6  select col,
      7    to_number(substr(col, 1, length(col) - 2)) result_1,
      8    to_number(col) / 100 result_2
      9  from test;
    
    COL     RESULT_1   RESULT_2
    ----- ---------- ----------
    100            1          1
    12500        125        125
    300            3          3
    
    SQL>
    
    • the first one removes the last two characters (from your sample data, it seems that they are always 00)
    • the second one divides that "number" by 100