您可以使用to_number() 和合适的format model 将字符串转换为数字:
to_number(your_string, '999,999,999')
具有适当数量的组和组分隔符。我假设,鉴于您正在寻找高于 5000 的值,逗号表示字符串中的组分隔符,而不是小数分隔符。避免依赖会话 NLS 设置会更安全,更明确一点:
to_number(replace(your_string, ' '), '999G999G999', 'NLS_NUMERIC_CHARACTERS=''.,''')
您的示例数据中的轻微复杂性是字符串中也存在空格。您可以使用replace() 将其删除:
to_number(replace(your_string, ' '), '999,999,999')
所以你的情况可能是:
where to_number(replace(your_string, ' '), '999,999,999') > 5000
或更安全:
where to_number(replace(your_string, ' '), '999G999G999', 'NLS_NUMERIC_CHARACTERS=''.,''')
> 5000
使用来自 CTE 的字符串进行演示:
with t (n) as (
select '43,042 ' from dual
union all select '- 1' from dual
union all select '44,889' from dual
union all select '35,224' from dual
union all select '- 1,000' from dual
union all select '17,683' from dual
)
select n, to_number(replace(n, ' '), '999,999,999') as converted
from t
where to_number(replace(n, ' '), '999,999,999') > 5000;
N CONVERTED
------- ----------
43,042 43042
44,889 44889
35,224 35224
17,683 17683
或更安全:
with t (n) as (
select '43,042 ' from dual
union all select '- 1' from dual
union all select '44,889' from dual
union all select '35,224' from dual
union all select '- 1,000' from dual
union all select '17,683' from dual
)
select n, to_number(replace(n, ' '), '999G999G999', 'NLS_NUMERIC_CHARACTERS=''.,''')
as converted
from t
where to_number(replace(n, ' '), '999G999G999', 'NLS_NUMERIC_CHARACTERS=''.,''') > 5000;
当然,您一开始就不应该将数字存储为字符串。使用正确的数据类型,您就不必处理此类问题。您也不必担心不是您期望的格式的字符串,因此可能由于多种原因导致 ORA-01722 错误......即使是没有组分隔符的数字,或者在错误的地方,如果你说它们应该在某些地方,就会出错。