在 Oracle 中,我有一个包含列 (X) 的表,其中可以包含如下字符串:
97M
481
101X
88
21E
等
我只想选择 x > 90 的整数值的那些行。在这个例子中,我希望取回包含值 97M、101X 和 481 的行。我该怎么做?
我使用REGEXP_REPLACE在使用之前删除了字母字符,TO_NUMBER
以便可以根据需要过滤结果:
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
希望能帮助到你...
您始终可以使用 translate 来删除字母字符。
TO_NUMBER(translate('90F', '1ABCDEFGHIJKLMNOPQRSTUFWXYZ', '1')) -- = 90
Translate 将第二个参数中的字符一对一翻译为第三个参数中的字符。
这是一个不同的例子。
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
您可以尝试以下方法:
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 开头,或者前三位数字是否为字符串。