我有一个带有列的表"描述"它具有以下值:
因此,获取值的select语句只是
SELECT description
FROM operation;
我想提取数字" 1010" (或任何与substr()标准匹配的字符串)并转换"找到的字符串"如果可能的话,变成一个整数。
所以我想出了这个:
SELECT to_number(substr(description, 3, 4))
FROM operation
WHERE regexp_like(substr(description, 3, 4), '^\d+(\.\d+)?$', '')
结果简单明了:" 1010"
对我来说效果很好。
现在对我来说很困难:我想在WHERE子句中使用substr() - 结果
这样的事情:
SELECT to_number(substr(description, 3, 4))
FROM operation
WHERE regexp_like(substr(description, 3, 4), '^\d+(\.\d+)?$', '')
AND substr(description, 3, 4) < 2000;
当我这样做时,我收到错误&#34;无效的号码&#34;。我想这是因为服务器解析select语句的顺序。
如果你能提供任何帮助,那就太棒了!!
答案 0 :(得分:2)
substr
函数返回一个字符串,您必须像在select语句中那样显式地将其强制转换为数字:
AND to_number(substr(description, 3, 4)) < 2000;
答案 1 :(得分:0)
SELECT to_number(substr(description, 3, 4))
FROM operation
WHERE regexp_like(substr(description, 3, 4), '^\d+(\.\d+)?$', '')
AND to_number(substr(description, 3, 4)) < 2000;
第二个to_number有帮助吗?
如果不是我会这样做:
select to_number(x) from (
SELECT substr(description, 3, 4) x
FROM operation
WHERE regexp_like(substr(description, 3, 4), '^\d+(\.\d+)?$', ''))
WHERE to_number(x) < 2000;