2013-10-29 34 views
3

如何转换字符串字段并将其用于Where clause.Am获取这样的异常请帮助找到错误的东西。将字符串转换为int,并在Where子句中使用

select * from student 
where (cast (nvl(linerevnum,'0') as int)) = 1 

linerevnum是VARCHAR2

例外: 无效号码

+3

这是因为,一些行,在该列中存储有非数字值。 – danihp

+0

即使检查null也发生同样的事情。谢谢你的回复。更新了问题。 – sunleo

+0

他从来没有说过null,他说非数字值...阅读兼容性在这里http://docs.oracle.com/javadb/10.6.2.1/ref/rrefsqlj33562.html – SriniV

回答

5

仅是数字

select * from student 
where 
    (
    case when ISNUMERIC(linerevnum) 
    then cast (linerevnum as int) 
    else null 
    end ) = 1 

或简单,当比较:

select * from student 
linerevnum = '1' 
+0

谢谢你,你是对的。到'null'该列中的字符串。 – sunleo

+4

GOOGLERS !!!在'CAST() – webLacky3rdClass

+0

@ webLacky3rdClass,MYSQL?'中更改'int'为'SIGNED','UNSIGNED'或'DECIMAL(xx)'bare int无效。谁在谈论MYSQL?它是[oracle cast](https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions016.htm)函数。 GOOGLERS是一种侮辱吗? UNREADERS,在写评论之前阅读标签;) – danihp