Oracle:将字符串转换为int

时间:2012-06-20 21:21:57

标签: sql oracle plsql

在Oracle中,我有一个包含列(X)的表,它可以包含这样的字符串:

97M
481个
101X
88个
21E
等。

我想只选择那些x> 1的整数值的行90.在这个例子中,我希望得到包含值97M,101X和481的行。我该怎么办?

3 个答案:

答案 0 :(得分:6)

我在使用TO_NUMBER之前使用REGEXP_REPLACE删除了字母字符,因此我可以根据需要过滤结果:

WITH t
  AS (SELECT '97F' AS x FROM DUAL
      UNION
      SELECT '481' FROM dual
      UNION
      SELECT '101A' FROM dual
      UNION
      SELECT '101A' FROM dual
      UNION
      SELECT '88' FROM dual
      UNION
      SELECT '21E' FROM dual)
SELECT x
  FROM t
 WHERE TO_NUMBER(regexp_replace(x, '[[:alpha:]]', '')) > 90;

X
101A
481
97F

希望它有所帮助...

答案 1 :(得分:2)

您始终可以使用翻译删除字母字符。

TO_NUMBER(translate('90F', '1ABCDEFGHIJKLMNOPQRSTUFWXYZ', '1')) -- = 90

Translate将第二个参数中的字符1对1转换为第三个参数中的字符。

这是一个不同的例子。

translate('ABCDEFG', 'ABC', 'XYZ') = 'XYZDEFG'

A -> X
B -> Y
C -> Z

现在,如果你看一下我的例子

translate('90F', '1ABCDEFGHIJKLMNOPQRSTUFWXYZ', '1')

1 -> 1 (this is here because if that last argument is null, you'll get an empty string)
A -> ? there's nothing here, so oracle will translate it to nothing
B -> same as above

答案 2 :(得分:0)

您可以尝试以下方法:

WHERE (substr(x, 1, 1) = '9' and substr(x, 2, 1) between '1' and '9'
      ) or
      (substr(x, 1, 1) between '1' and '9' and
       substr(x, 2, 1) between '0' and '9' and
       substr(x, 3, 1) between '0' and '9'
      )

这有点蛮力。它会检查字符串是否以91-99开头,或前三个数字是否为字符串。