Case 表达式抛出异常

Case Expression throwing exception

考虑以下代码:

 Select FLAGS,
 CASE FLAGS 
 WHEN 0 THEN "10000000"
 WHEN 1 THEN "01000000"
 WHEN 2 THEN "00100000"
 WHEN 3 THEN "00010000"
 WHEN 4 THEN "00001000"
 WHEN 5 THEN "00000100"
 WHEN 6 THEN "00000010"
 WHEN 7 THEN "00000001"
 ELSE "00000000"
 END AS Test-W 
 FROM V_TEST

以上语句抛出错误为:

ORA-00923: FROM keyword not found where expected
 00923. 00000 -  "FROM keyword not found where expected"
 *Cause:    
 *Action: Error at Line: 14 Column: 17

我的 table 名字是 V_TEST,列名是 FLAGS。我在这里做错了什么?

对文字表达式使用单引号 `

Test-W 列需要用双引号引起来

Select FLAGS,
 CASE FLAGS 
 WHEN 0 THEN '10000000'
 WHEN 1 THEN '01000000'
 WHEN 2 THEN '00100000'
 WHEN 3 THEN '00010000'
 WHEN 4 THEN '00001000'
 WHEN 5 THEN '00000100'
 WHEN 6 THEN '00000010'
 WHEN 7 THEN '00000001'
 ELSE '00000000'
 END as "Test-W"
FROM V_TEST

当然,CASE 冗长且易于理解。您还可以使用 DECODE -

编写相同的查询
SELECT FLAGS, 
       DECODE(FLAGS, 
       0 , '10000000',
       1 , '01000000',
       2 , '00100000',
       3 , '00010000',
       4 , '00001000',
       5 , '00000100',
       6 , '00000010',
       7 , '00000001',
           '00000000')
  AS TEST-W 
 FROM V_TEST
/