我有一个varchar colunm。内容
ColumnX
________
ABC
DEF
1
2
3
4
40
50
我需要获得1到4之间的数字。所以我有这个SQL
SELECT columnX
FROM table
WHERE regexp_substr(columnX, '[[:digit:]]') BETWEEN 1 and 4;
但我得到的结果是1,2,3,4和40。 我该怎么做才能做到正确?
答案 0 :(得分:1)
您必须使用量词来匹配超过1个字符。在这种情况下,我使用+
量词,它匹配一个或多个字符出现(在这种情况下为数字)。
试试这个:
WITH
test_data AS
(SELECT 'ABC' AS columnX FROM dual
UNION ALL SELECT '1' FROM dual
UNION ALL SELECT '2' FROM dual
UNION ALL SELECT '3' FROM dual
UNION ALL SELECT '4' FROM dual
UNION ALL SELECT '40' FROM dual
UNION ALL SELECT '50' FROM dual
)
SELECT columnX
FROM test_data
WHERE regexp_substr(columnX, '[[:digit:]]+') BETWEEN 1 AND 4;
输出:
COLUMNX ------- 1 2 3 4