甲骨文解码功能相当于在SQL服务器2005+

问题描述:

可能重复:
DECODE() function in SQL Server甲骨文解码功能相当于在SQL服务器2005+

有没有在SQL Server Oracles Decode功能(2005或更高版本)的直接等同?

如果没有,是否有另一种方法来实现与DECODE相同的效果?

等价的SQL命令是一个CASE语句:

例如:

declare @t table(id int identity,code varchar(25)) 
insert @t(code) 
    select 'A' UNION ALL 
    select 'B' UNION ALL 
    select 'C' UNION ALL 
    select 'D' UNION ALL 
    select 'E' UNION ALL 
    select 'F' 
    select * from @t 
    -- 
    select id,code, 
     case id 
       when 1 then code++convert(varchar(10),id) 
       when 2 then code++convert(varchar(10),id) 
       else 'UNDEFINED-DECODE VALUE for id : '+convert(varchar(10),id) 
     end as DECODEDVALUE 
    from @t 
    -- 

返回此数据集:

id   code      DECODEDVALUE 
----------- ------------------------- ------------------------------------------ 
1   A       A1 
2   B       B2 
3   C       UNDEFINED-DECODE VALUE for id : 3 
4   D       UNDEFINED-DECODE VALUE for id : 4 
5   E       UNDEFINED-DECODE VALUE for id : 5 
6   F       UNDEFINED-DECODE VALUE for id : 6