我正在尝试使用内部的子查询和Oracle Decode语句,如下所示
RPAD(NVL(DECODE(TRIM(ST.StudentCode),'AB','CA','TM','CH',(Select InternalNumber from Address where State = SA.STATECODE) <=2,'PAS', ST.StudentCode), ' '), 3, ' ')
当我使用原始查询运行此部件时,我在同一行中收到“Missing right paranthesis”错误。这里出了什么问题?
答案 0 :(得分:1)
您可以在子查询中添加case expression:
RPAD(NVL(DECODE(TRIM(ST.StudentCode),'AB','CA','TM','CH',CASE WHEN (Select InternalNumber from Address where State = SA.STATECODE) <=2 THEN 'PAS' ELSE ST.StudentCode END), ' '), 3, ' ')
Oracle数据库搜索第一个WHEN ... THEN对,其expr等于comparison_expr并返回return_expr。如果没有WHEN ... THEN对满足此条件,并且存在ELSE子句,则Oracle返回else_expr。否则,Oracle返回null。
答案 1 :(得分:1)
只需使用一个case
表达式:
RPAD( (CASE WHEN TRIM(ST.StudentCode) = 'AB' THEN 'CA',
WHEN TRIM(ST.StudentCode) = 'TM' THEN 'CH',
WHEN (Select a.InternalNumber from Address a where a.State = SA.STATECODE) <= 2 THEN 'PAS'
ELSE COALESCE(ST.StudentCode, ' '),
), 3, ' ')