查询有关nvl和case语句的信息

时间:2016-11-09 11:42:41

标签: sql oracle

1

select   case 
        when  id is null then name='muthu'
        end 
from muthu;
执行此查询后

获取ORA-00905:缺少关键字

  
      
  1. 00000 - “缺少关键字”错误。
  2.   

2

select nvl(id,"not found")  name from muthu;
  

ORA-00904:“未找到”:标识符无效

(oracle 10g db和sql developer 4.1。) 请任何人都可以告诉我最新版本的sql developer

2 个答案:

答案 0 :(得分:0)

您的第一个查询失败,因为布尔表达式不是。我想你想要:

select (case when id is null then 'muthu'
        end) as name
from muthu;

第二个失败,因为字符串应该用单引号括起来,而不是双引号。所以这应该是:

select nvl(id, 'not found') as name
from muthu;

虽然我更喜欢ANSI标准coalesce()到特定于数据库的NVL()

如果id不是字符串,则必须将其强制转换为字符串:

select nvl(cast(id as varchar2(255)), 'not found') as name
from muthu;

答案 1 :(得分:0)

不确定是什么问题,我假设为什么?

首次查询 - 我想您想将此列命名为select case when id is null then 'muthu' end as name from muthu; ?如果是这样,在计算结束时或列上的任何内容都会出现命名:

select nvl(id,'not found') as name
from muthu;

第二个 - 字符串应该用单引号括起来,双引号用于列名:

{{1}}