September 9, 2008 at 3:14 am
How can I do a conditional select in my queries?
I'm trying to do this:
SELECT Field1, Field2, IF Field3 > GetDate() THEN Field3 ELSE Field4, Field5 ..........
Which is failing. How would I go about structuring this logic?
Thanks a lot everyone!
September 9, 2008 at 4:04 am
Hi Chris
IF is for conditional execution of statements within a batch. Use CASE for conditions within a statement. Check out CASE in BOL, there are two types, simple and searched. Looks like you will need the searched type:
CASE WHEN Field3 > GetDate() THEN...
as opposed to the simple type,
CASE Field3 WHEN...
Cheers
ChrisM
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
September 9, 2008 at 4:09 am
Thats spot on mate - thanks a lot! 😀
September 9, 2008 at 4:21 am
You're welcome Chris, thanks for the feedback!
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply