Oracle:to_number() 与 WHERE 子句中的 substr() 和 regexp_like() 组合
Oracle: to_number() combined with substr() and regexp_like() in WHERE clause
我有一个 table,其列 "description" 具有以下值:
- OPTestMachine
- OPManualTesting
- OP1010
所以 select 语句,获取值,只是
SELECT description
FROM operation;
我想提取数字“1010”(或任何符合 substr() 条件的字符串)并尽可能将 "found string" 转换为整数。
所以我想到了这个:
SELECT to_number(substr(description, 3, 4))
FROM operation
WHERE regexp_like(substr(description, 3, 4), '^\d+(\.\d+)?$', '')
结果简单明了:“1010”
这对我来说效果很好。
现在对我来说最困难的部分是:我想在 WHERE 子句中使用 substr()-result
像这样:
SELECT to_number(substr(description, 3, 4))
FROM operation
WHERE regexp_like(substr(description, 3, 4), '^\d+(\.\d+)?$', '')
AND substr(description, 3, 4) < 2000;
执行此操作时出现错误 "Invalid number"。我想这是因为服务器解析 select 语句的顺序。
如果你能提供任何帮助那就太好了!!
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;
substr
函数 returns 一个字符串,您必须像在 select 语句中那样将其显式转换为数字:
AND to_number(substr(description, 3, 4)) < 2000;
我有一个 table,其列 "description" 具有以下值:
- OPTestMachine
- OPManualTesting
- OP1010
所以 select 语句,获取值,只是
SELECT description
FROM operation;
我想提取数字“1010”(或任何符合 substr() 条件的字符串)并尽可能将 "found string" 转换为整数。
所以我想到了这个:
SELECT to_number(substr(description, 3, 4))
FROM operation
WHERE regexp_like(substr(description, 3, 4), '^\d+(\.\d+)?$', '')
结果简单明了:“1010”
这对我来说效果很好。
现在对我来说最困难的部分是:我想在 WHERE 子句中使用 substr()-result
像这样:
SELECT to_number(substr(description, 3, 4))
FROM operation
WHERE regexp_like(substr(description, 3, 4), '^\d+(\.\d+)?$', '')
AND substr(description, 3, 4) < 2000;
执行此操作时出现错误 "Invalid number"。我想这是因为服务器解析 select 语句的顺序。
如果你能提供任何帮助那就太好了!!
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;
substr
函数 returns 一个字符串,您必须像在 select 语句中那样将其显式转换为数字:
AND to_number(substr(description, 3, 4)) < 2000;