Home > Software design >  Problematic Case Statement in Db2
Problematic Case Statement in Db2

Time:02-04

I have the following prompt regarding the sample database Db2 comes with.

List the full names (first, middle, last) and education level (column labelled EDUCATION) of all employees. The education level should be printed as 'GRADUATE' if 18 or greater; 'COLLEGE' if 16 or greater; and 'HIGH SCHOOL’ if less than 16. The column headings should be FIRST, M, LAST, EDUCATION.

Here is my work:

SELECT firstnme AS first, midinit AS m, lastname AS last, 
  CASE 
    WHEN edlevel >= 18 THEN "GRADUATE" 
    WHEN edlevel < 16 THEN "HIGH SCHOOL" 
    ELSE "COLLEGE"
  END AS education 
FROM employee;

Here is the error message:

SQL0206N  "GRADUATE" is not valid in the context where it is used.  
SQLSTATE=42703

It works if I assign dummy integer values instead of "GRADUATE", "HIGH SCHOOL", and "COLLEGE", so it sounds like there is just some some very string-specific issue. Also, edlevel is originally a smallint. So, is my problem really the same one as this?

CodePudding user response:

You are using double quotes instead of single quote for string value. You could try this

SELECT firstnme AS first, midinit AS m, lastname AS last, 
  CASE 
    WHEN edlevel >= 18 THEN 'GRADUATE' 
    WHEN edlevel < 16 THEN 'HIGH SCHOOL' 
    ELSE 'COLLEGE'
  END AS education 
FROM employee;
  • Related