Search code examples
oracle-databasecase-expression

Case Expression throwing exception


Consider the following code:

 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

The above statement is throwing error as:

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

My table name is V_TEST, column name is FLAGS. What am I doing wrong here?


Solution

  • use single quote ` for the literal expressions

    The column Test-W needs to be enclosed in dobule quote

    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