Franz
Franz

Reputation: 1

Use If inside a Select - Where

I was wondering: Is it possible to use an IF statement inside a WHERE clause for a SELECT? Here's a sample:

SELECT var1, var2, .. AS name
FROM table1 C, table2 F, table3 D,.. 
WHERE C.id = f.id, AND ..
AND (IF expression THEN F.num <0; -- <-- is this possible?
     ELSE F.num >0);

Thanks in advance

Upvotes: 0

Views: 60

Answers (3)

Valli
Valli

Reputation: 1450

You should be able to do without if statement.

Where (expression And num >0)
       Or ( not( expression) and num<0)

Upvotes: 0

Franz
Franz

Reputation: 1

I found a way. here's the solution

SELECT
.
.
WHERE ..
AND SIGN(F.NUM) = DECODE(expression,-1,1);

Simpler than I thought. Sorry for the trouble

Upvotes: 0

Sabarish Mahalingam
Sabarish Mahalingam

Reputation: 145

IN THIS SCENARIO: USE CASE STATMENT

SELECT var1, var2, .. AS name
FROM table1 C, table2 F, table3 D,.. 
WHERE C.id = f.id, AND ..
AND COL = (CASE WHEN F.NUM <0 THEN (UR COL) 
                WHEN ELSE F.num > 0 ELSE '' END)

Above Code is only Sample, Try to apply your own scenario.

Upvotes: 1

Related Questions