SELECT name, CASE WHEN type = 1 THEN 'A' WHEN type = 2 THEN 'B' ELSE 'C' END AS typeflag FROM custtable WHERE typeflag = 'B'
I know this can be done by doing "where type = 2" but I can't give you an easier example. When I try this, SQL says "typeflag is not a column in the table."
To do this on a single statement, you can use an SQL common table expression. I find table expressions helpful in breaking a complex SQL statement into smaller, simpler components. Here's an example of implementing your statement with a common table expression:
WITH custexp AS (SELECT name, CASE WHEN type = 1 THEN 'A' WHEN type = 2 THEN 'B' ELSE 'C' END AS typeflag FROM custtable) SELECT name, typeflag FROM custexp WHERE typeflag='B'
Dig deeper on iSeries SQL commands and statements
Related Q&A from Kent Milligan
IBM did not change the default of the reuse deleted files function to YES for physical files in order to minimize the impact on any existing ...continue reading
To solve the SQL error -321 on IBM i6.1, use the new values statement to overcome the error. If you are using an older release, declare a cursor ...continue reading
When working with DB2 files with columns that have both short and long names, there is no option choose which column names are returned via ODBC ...continue reading
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.