Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IF Statement

Status
Not open for further replies.

n36z

Programmer
Jul 26, 2011
13
US
Expression (A) Successfully executes, however Expression B returns an error. I want to be able to
conditionaly include a WHERE or AND clause based off of a specified condition. How do I do it?


Expression A
if (2>1)
select *
from table1
where columnA = 2

Expression B
select *
from table1
if (2>1)
where columnA = 2
 
You can't conditionally include or exclude clauses like you're trying but you can use a case statement to conditionally apply choose one or both sides of the clause so...

Code:
select
    *
from
    table1 
where 
    case
        when 2>1 then columnA 
        else 1
    end = 2
...or...
Code:
select
    *
from
    table1 
where 
    case
        when 2>1 then columnA 
        else 1
    end = 
    case
        when 2>1 then 2 
        else 2
    end

...or something very close to that will work, (I don't have anywhere to check it at the moment)

Rhys

"The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it"
Terry Pratchett
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top