CASE Expressions

You can add if–then–else logic to your SQL statements and output using CASE expressions. Consider the generation of a list of those candidates who have passed the DB2 Fundamentals exam. In the report, you want to print the score of the tests, but instead of printing the numeric score, you want to print a message. If the score is below the 65, you want to print Not Passed. If it is between the 65 and 90, you want to print Passed, and if the score is above 90, you want to print Excellent. The following SQL statement using a CASE expression accomplishes this:

 SELECT fname,lname, CASE WHEN SCORE < 65 THEN 'Not Passed' WHEN SCORE <= 90 THEN 'Passed ' ELSE 'Excellent' END AS TEST_RESULT FROM candidate c, test_taken tt WHERE c.cid=tt.cid ...

Get DB2® Universal Database™ for OS/390™ v7.1 Application Certification Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.