imported_jediknight

Senior member
Jun 24, 2004
343
0
0
Need to create a query as above. How can I do this in DB2?

My thought is:
SELECT not exists (select * from table) as answer;

But that doesn't work. The only other thing I can think of is something along the lines of:
select 'true' as answer
from t
where c=999 and 5 in (1,2,3)
union
select 'false' as answer
from t
where c=999 and 5 not in (1,2,3);
(NB: t is a table that has one entry where c=999)

But something similar (using exists, not exists) for my query above seems like a real hack.. and I think there should be a better way to accomplish this.

Thanks..
 

edmicman

Golden Member
May 30, 2001
1,682
0
0
can't you count the number of rows in a table, and if that is greater than 0 then it has records?
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
SELECT CASE WHEN c = 999 AND 5 in (1,2,3) THEN 'True' ELSE 'False' FROM t

Although, what is "5 in (1,2,3)". Surely 5 is not a valid column name ...
 

imported_jediknight

Senior member
Jun 24, 2004
343
0
0
Originally posted by: MrChad
SELECT CASE WHEN c = 999 AND 5 in (1,2,3) THEN 'True' ELSE 'False' FROM t

Although, what is "5 in (1,2,3)". Surely 5 is not a valid column name ...


Thanks. Surely, 5 is not a column name.. it's just there for demonstration purposes.. the whole query is much longer (and rather pointless to post).
 

cyberia

Platinum Member
Oct 22, 1999
2,535
0
0
I've never dealt with DB2, but this works on MS SQL Server.

SELECT CASE
WHEN COUNT(*) > 0 THEN 'True'
ELSE 'False'
END
AS answer
FROM t
 

Cheetah8799

Diamond Member
Apr 12, 2001
4,508
0
76
Rather than having it return true or false, what I do when I code in PHP and MySQL is I simply have it check how many rows were returned. If > 0, then that is the same as false for your example.
 

Jeraden

Platinum Member
Oct 9, 1999
2,518
1
76
This will work in oracle, dunno about db2:

select decode(count(0),0,'True','False')
from table