31

如何将 oracle varchar 值转换为数字

例如

table - exception
exception_value 555 where exception_value is a varchar type

我想测试 exception_value 列的值

select * from exception where exception_value = 105 instead of
select * from exception where exception_value = '105'
4

5 回答 5

42

您必须使用TO_NUMBER函数:

select * from exception where exception_value = to_number('105')
于 2009-07-20T16:45:19.373 回答
13

由于列是 VARCHAR 类型,因此您应该将输入参数转换为字符串,而不是将列值转换为数字:

select * from exception where exception_value = to_char(105);
于 2009-07-21T10:33:02.903 回答
3

If you want formated number then use

SELECT TO_CHAR(number, 'fmt')
   FROM DUAL;

SELECT TO_CHAR('123', 999.99)
   FROM DUAL;

Result 123.00

于 2013-11-18T10:07:55.520 回答
3

我已经测试了建议的解决方案,它们应该都可以工作:

select * from dual where (105 = to_number('105'))

=> 提供一个虚拟行

select * from dual where (10 = to_number('105'))

=> 空结果

select * from dual where ('105' = to_char(105))

=> 提供一个虚拟行

select * from dual where ('105' = to_char(10))

=> 空结果

于 2017-12-18T15:28:59.310 回答
1

从 to_number(exception_value) = 105 的异常中选择 to_number(exception_value)

于 2009-07-20T16:43:49.843 回答