tags:

views:

109

answers:

8

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

+4  A: 

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

AvatarKava
A: 

Use the SQL CASE statement

Shiraz Bhaiji
A: 

There is a IF block for SQL.

http://sqltutorials.blogspot.com/2007/06/sql-ifelse-statement.html

Daniel A. White
+7  A: 
SELECT CASE WHEN Col1 > 25 THEN 'Y' ELSE 'N' END As Value1
FROM Table1
Nebakanezer
A: 
SELECT CASE WHERE columnname > 25 THEN 'Y' ELSE 'N' END FROM table;
Cahlroisse
A: 
select 
case when somecolumn > 25 then 'Y' else 'N' end as somename
from sometable;
tpdi
A: 

Select If(myColumn > 25, 'Y', 'N') From myTable

Neil N
A: 

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

Cruachan