Is there a way to have a query return either a 'Y' or 'N' for a column if that column has a value above a certain number say '25'.
I am using DB2 on ZOS
Is there a way to have a query return either a 'Y' or 'N' for a column if that column has a value above a certain number say '25'.
I am using DB2 on ZOS
For MySQL:
SELECT IF(column > 25, 'Y', 'N')
FROM table
http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function%5Fif
There is a IF
block for SQL.
http://sqltutorials.blogspot.com/2007/06/sql-ifelse-statement.html
SELECT CASE WHEN Col1 > 25 THEN 'Y' ELSE 'N' END As Value1
FROM Table1
SELECT CASE WHERE columnname > 25 THEN 'Y' ELSE 'N' END FROM table;
select
case when somecolumn > 25 then 'Y' else 'N' end as somename
from sometable;
Apart from the IF and CASE statements another valid alternative is to create a user defined function feeding in your column value and returning Y or N.
This has the not inconsiderable advantage that if you are selecting on this criteria in more than on SQL statement and at a later date your condition changes - to over 30 say - then you have only one place to change your code. Although using a function adds complexity and overhead so is not always optimal don't underestimate the maintenance advantage of this approach. A minor plus too is you can make the name of the function something more meaningful, and hence self documenting, than an inline CASE