user42117
user42117

Reputation: 35

Need help to modify my SQL query

I want to modify my existing SQL query (number 1) given below. Firstly, I need to know if it is possible to do what I want (number 2). If yes, then what are the ways I can do it.

--**(Number 1)** What I currently have      

SELECT  Column1
    FROM  dbo.Table1
    WHERE ColumnValue1 = 'One'
    AND scalarFunctionResult(Var) = 'Word1'
    AND ColumnValue2 = "Word2"


--*(Number 2)* What I need      
SELECT  Column1
    FROM  dbo.Table1
    WHERE ColumnValue1 = 'One'
    AND SomeVariable IN (scalarFunctionResult(Var) = 'Word1', 'WordA')
-- Perform a query based 
--on value of SomeVariable, Please read pseudo code below 
    AND ColumnValue2 = "Word2"

--Pseudo code for logic
--if(SomeVariable = 'Word1') then run query ABCD,
--else if(SomeVariable = 'WordA') then run some other query EFGH

Upvotes: 0

Views: 101

Answers (2)

David Smithers
David Smithers

Reputation: 2364

Are you trying to figure out how to create IF statement in SQL? Not sure I understand your question correctly.

If this is so here is the reference from MSDN . You might not need it in this case though as RichardTheKiwi showed.

Upvotes: 0

RichardTheKiwi
RichardTheKiwi

Reputation: 107736

It's a simple OR

SELECT Column1
  FROM dbo.Table1
 WHERE ColumnValue1 = 'One'
   AND ((@SomeVariable = 'Word1' AND <the condition ABCD>)
        OR
        (@SomeVariable = 'WordA' AND <the condition EFGH>))

Effectively, you get the result from either ABCD when @Somevariable = 'Word1', or from the other condition "query" EFGH when @Somevariable = 'WordA'.

e.g.

SELECT Column1
  FROM dbo.Table1
 WHERE ColumnValue1 = 'One'
   AND ((dbo.scalarFunctionResult(@SomeVariable) = 'Word1' AND ColumnValue2 = "Word2")
        OR
        (dbo.scalarFunctionResult(@SomeVariable) = 'WordA' AND ColumnValue3 = "Word3"))

Upvotes: 3

Related Questions