“Oracle SQL开发人员”;ORA-01722:无效编号“;

“Oracle SQL开发人员”;ORA-01722:无效编号“;,sql,oracle,Sql,Oracle,在尝试运行查询时,我不断收到此错误“ORA-01722:无效号码”。为了避免这个错误,我已经多次重写了这个查询,但似乎做不到。这必须是一个案例陈述,在这里我将通过GPA小时数来比较班级水平。当然有人对我的问题有一些建议或答案!提前谢谢 select distinct initcap(stride_last_name), stride_first_name, substr(stride_middle,1,1), case when nvl(su

在尝试运行查询时,我不断收到此错误“ORA-01722:无效号码”。为了避免这个错误,我已经多次重写了这个查询,但似乎做不到。这必须是一个案例陈述,在这里我将通过GPA小时数来比较班级水平。当然有人对我的问题有一些建议或答案!提前谢谢

select distinct 
    initcap(stride_last_name),
    stride_first_name, 
    substr(stride_middle,1,1), 
    case
        when nvl(sum(gpa_hours_earned),'0') between '0' and '29.9' then 'Freshmen'                   
        when nvl(sum(gpa_hours_earned),'0') between '30' and '59.9' then 'Sophomore'   
        when nvl(sum(gpa_hours_earned),'0') between '60' and '89.9' then 'Junior' 
        when nvl(sum(gpa_hours_earned),'0') >= '90' then 'Senior' 
        else 'Freshmen' 
    end

假设您的
gpa\u hours\u earned
列是数字,这是因为您将总和与字符串进行比较,这意味着像
'29.9'
这样的字符串文本将被隐式转换为数字,并且会话设置为以逗号作为十进制分隔符,以句点作为组分隔符。您可以更改会话NLS\u数字\u字符设置,或使用指定小数分隔符的显式转换

但是您不应该对数字使用文本文字。删除它们周围的引号:

select distinct 
    initcap(stride_last_name),
    stride_first_name, 
    substr(stride_middle,1,1), 
    case
        when nvl(sum(gpa_hours_earned),0) between 0 and 29.9 then 'Freshmen'                   
        when nvl(sum(gpa_hours_earned),0) between 30 and 59.9 then 'Sophomore'   
        when nvl(sum(gpa_hours_earned),0) between 60 and 89.9 then 'Junior' 
        when nvl(sum(gpa_hours_earned),0) >= 90 then 'Senior' 
        else 'Freshmen' 
    end
由于实际查询必须具有GROUPBY子句,因此不需要使用
distinct
。您可以简化该情况,因为它使用短路评估:

    case
        when nvl(sum(gpa_hours_earned),0) < 30 then 'Freshmen'                   
        when nvl(sum(gpa_hours_earned),0) < 60 then 'Sophomore'   
        when nvl(sum(gpa_hours_earned),0) < 90 then 'Junior' 
        else 'Senior'
    end
案例
当nvl(总gpa学时数)小于30时,则为“新生”
当nvl(总gpa时数)小于60时,则为“二年级”
当nvl(总gpa小时数)小于90时,则为“初级”
其他“高级”
结束

谢谢你的帮助,非常感谢!不幸的是,在接受了你的建议之后,它仍然在向我抛出错误代码。我不确定到底是什么原因造成的!我删除了distinct,取出了字符串文本,并按照您的建议重新格式化了case语句。你认为我可以尝试下一步吗?
gpa\u hours\u reaned
实际上是数字列还是字符串?如果它是一个字符串,则几乎可以肯定它不应该是字符串,并且可能包含无法转换的错误值。@GMitchell-如果它是一个数字,您能否编辑该问题以显示整个(原始)查询、表定义和完整的错误消息?错误可能来自您尚未显示的内容。是的,它是数字列。SHRTGPA_小时数不为空(9,3)。完整错误消息>>ORA-01722:无效号码01722。00000-“无效数字”*原因:*措施:我将发布查询,给我一秒钟时间