0

Is it possible to use IF ELSE inside WHERE clause something like this...

WHERE transactionId like @transactionId
AND transactionType like @transactionType
AND customerId like @customerId
IF(@transactionType='1')
AND qTable.statusId like 'SIGNED'
ELSE
AND rTable.statusId like 'ACTIVE'
shree.pat18
  • 21,449
  • 3
  • 43
  • 63
Reynan
  • 261
  • 2
  • 9
  • 23
  • possible duplicate of [SQL: IF clause within WHERE clause](http://stackoverflow.com/questions/87821/sql-if-clause-within-where-clause) – Alberto Solano Apr 11 '14 at 08:03

3 Answers3

2

You cannot use the IF ELSE statement within the WHERE clause. Use CASE instead:

WHERE transactionId like @transactionId
AND transactionType like @transactionType
AND customerId like @customerId
AND CASE WHEN @transactionType = '1' THEN qTable.statusId like 'SIGNED' 
    ELSE rTable.statusId like 'ACTIVE' END
Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
1
WHERE transactionId like @transactionId
AND transactionType like @transactionType
AND customerId like @customerId
AND 
  ((@transactionType='1' AND qTable.statusId like 'SIGNED')
OR 
  (@transactionType <> '1' AND like 'ACTIVE') )
Milen
  • 8,697
  • 7
  • 43
  • 57
0

Is this what you need?

WHERE transactionId like @transactionId
AND transactionType like @transactionType
AND customerId like @customerId
AND ((transactionType='1' and qTable.statusId like 'SIGNED') or (transactionType <> '1' and rTable.statusId like 'ACTIVE'))
Esko
  • 4,109
  • 2
  • 22
  • 37