TRIM尾部零点

问题描述 投票:0回答:2

我知道这是一个相对简单的问题,但我无法找到任何与我的关键字使用Google.我是用一个SQL(Oracle)引用到一列,有这样的数字。

  • 100
  • 12500
  • 300

现在我需要去掉最后两个零。这个方法是行不通的。

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

谁有什么办法?

结果应该是一列数字--而不是文本

sql oracle trim
2个回答
1
投票

请看这两个选项。

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>
  • 第一个是去掉最后两个字符 (从你的样本数据来看,它们似乎总是... ...) 00)
  • 第二种是将这个 "数字 "除以 100

1
投票

你可以这样做。

SELECT regexp_replace(F0035.Nr, '^(.*)00$', '\1')
FROM F0035

如果你的要求发生了细微的变化,你可以很容易地调整正则表达式,比如去掉超过两个尾部的零(例如 ^(.*)00+),或者其他字符


1
投票
with test (col) as (
  select 10   from dual union all
  select 100  from dual union all
  select 1000 from dual union all
  select 12500 from dual union all
  select 125002 from dual union all
  select 3000   from dual
)
select col, 
case when substr(col, -2) = '00' then col/100 else col end newnum
from test;

1
投票

如果列中有数字,为什么要用字符串操作?

如果所有的值都有两个00作为结尾,那么。

F0035.Nr / 100

如果有些值没有,那就用 case:

(case when mod(F0035.Nr, 100) = 0 then F0035.Nr / 100 else F0035.Nr end)

我不建议在大多数情况下转换为字符串来进行数字运算。


1
投票

下面的表达式将从一个数字中去掉任何数量的零。

SELECT NR / POWER(10, LENGTH(REGEXP_SUBSTR(TO_CHAR(NR), '0*$')))
  FROM F0035

db<>fiddle here.

© www.soinside.com 2019 - 2024. All rights reserved.