Click here to Skip to main content
12,403,978 members (69,104 online)
Rate this:
Please Sign up or sign in to vote.
See more: SQL T-SQL
Hi All
I am facing a issue in applying a conditional 'if' in the where clause of the SQL query.
Here is the query. I have removed the unnecessary joins, columns names and multiple conditions in WHERE clause to make it readable.
SELECT TransactionId, TransactionDate , ProcessingDate FROM TransactionDetails
WHERE TransactionId in (2,3,9) AND ...AND ..
Now, I need to select the records from the TransactionDetails table with another transaction Ids 14 & 15 ONLY if the TransactionDate <> ProcessingDate for that particualr record.

What I tried:

Tried using IF ELSE - but it worked only with the variables. I want to apply the condition on the Dates columns from the table.

select  * from transactionDetails
WHERE OrderID in (400376, 400379)
    IF TransactionDate <> ProcessingDate
        TransactionId in (2,3,9,14,15)
        TransactionId in (2,3,9)

I get two errors as -
Incorrect syntax near the keyword 'IF'.
Incorrect syntax near 'TransactionId'.

Please comment if you want me to improve my question
Posted 4-Nov-12 20:28pm
Updated 4-Nov-12 20:30pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

select  * from transactionDetails
WHERE OrderID in (400376, 400379)
    ((TransactionDate <> ProcessingDate AND TransactionId in (2,3,9,14,15)
     (TransactionDate = ProcessingDate AND TransactionId in (2,3,9)))
aspnet_regiis -i 5-Nov-12 6:17am
It worked .. Thank you OG ..+5.. I wish I could have voted +50.. You saved me ..I have been scratching my head on this for a while...
OriginalGriff 5-Nov-12 6:59am
Um...SA? :laugh:
aspnet_regiis -i 5-Nov-12 7:11am
Sorry ..My mistake.. It was a typo.. Confused between the two great names on CP :)
Maciej Los 8-Nov-12 14:52pm
Umesh AP 30-Mar-16 6:03am
Thanks, Useful for me also.....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 5 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100