Click here to Skip to main content
11,920,353 members (54,765 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 21:28pm
Edited 4-Nov-12 21: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

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

  Print Answers RSS
0 OriginalGriff 415
1 phil.o 170
2 Dave Kreskowiak 155
4 F-ES Sitecore 130
0 OriginalGriff 6,778
1 KrunalRohit 4,495
2 Sergey Alexandrovich Kryukov 3,203
3 George Jonsson 2,805
4 Suvendu Shekhar Giri 2,181

Advertise | Privacy | Mobile
Web04 | 2.8.151120.1 | Last Updated 5 Nov 2012
Copyright © CodeProject, 1999-2015
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