编写查询以显示所有部门的部门ID,部门名称,经理ID。如果没有管理员ID,则显示“ NA”,并将别名命名为“ MANAGER_ID”。根据部门ID对结果进行排序。 manager_id的类型是数字。
select
department_id, department_name, manager_id, NVL(manager_id, 'na') "BONUS_AMT"
from
departments
order by 1;
是我的查询,但错误是第1行上的错误:
ORA-01722:无效的数字
需要帮助!
答案 0 :(得分:2)
当NVL
为NUMBER
时,您正在使用null
获取字符串。如果需要字符串结果,则需要将数字转换为字符串。例如:
SQL> select nvl(x, 'a') from (select 1 x from dual);
select nvl(x, 'a') from (select 1 x from dual)
*
ERROR at line 1:
ORA-01722: invalid
SQL> select nvl(to_char(x, '999'), 'a') from (select 1 x from dual);
NVL(TO_CHAR(X,'999'),'A')
--------------------------------------------------------------------------------
1
答案 1 :(得分:0)
select
department_id, department_name, manager_id,
NVL(cast(manager_id as varchar2(10)), 'na') "BONUS_AMT"
from
departments
order by 1;