甲骨文.将varchar值"40.00"转换为数字

Max*_*Max 3 sql oracle

例如,一个varchar值为"40.00",并希望在where子句中使用运算符">"或"<".我怎么用呢?

And*_*mar 5

您可以将varchar转换为十进制,如:

select *
from YourTable
where CAST(YourField AS decimal) < 40.00
Run Code Online (Sandbox Code Playgroud)

或者使用TO_NUMBER()函数:

select *
from YourTable
where TO_NUMBER(YourField) < 40.00
Run Code Online (Sandbox Code Playgroud)

如果该字段不总是数字,并且您有一个相对较新的Oracle安装,则可以选择YourField为数字的行,如:

select *
from (
    select * 
    from YourTable
    where regexp_like(YourField, '^-?[[:digit:],.]+$')
) sub
where TO_NUMBER(YourField) < 40.00
Run Code Online (Sandbox Code Playgroud)


Ton*_*ews 5

您可以使用TO_NUMBER函数将其转换为数字:

WHERE TO_NUMBER(col) > 39
Run Code Online (Sandbox Code Playgroud)

但请注意:如果表中的任何行在该列中具有非数字条目,则查询可能会失败并显示错误:

SQL> create table t (col varchar2(10));

Table created.

SQL> insert into t values ('39.00');

1 row created.

SQL> insert into t values ('40.00');

1 row created.

SQL> insert into t values ('41.00');

1 row created.

SQL> insert into t values ('xxx');

1 row created.

SQL> select * from t where to_number(col) > 39;
ERROR:
ORA-01722: invalid number
Run Code Online (Sandbox Code Playgroud)