SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Conditional Where Clause


Conditional Where Clause

Author
Message
al_anderson
al_anderson
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 96
I have 2 formats of date fields in 2 seperate tables. Given the selection of P or F
I am attempting to do use the appropriate date range for the where clause:

WHERE 1=1
AND CASE
WHEN FPCH.ProcessedDateKey >= @Beg_Date AND FPCH.ProcessedDateKey <= @End_Date THEN 'P'
WHEN AR.RunCycleDate>= @pBeg_Date AND AR.RunCycleDate<= @pEnd_Date THEN 'F'
END = @DateSolution
AND FPCH.InventoryStatusCode in (97,98)


The P seems to work, but the F doesn't. I'm self taught, and have been programming in other languages for a long time, but I'm not sure what I'm missing here. Any help would be greatly appreciated. Thanks in advance.
GSquared
GSquared
SSC-Dedicated
SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)

Group: General Forum Members
Points: 31507 Visits: 9730
Not sure from just this little bit, but a place to start is that it will never get to F if the test for P comes up true. Case statements stop as soon as they find a true condition.

Also, why have the 1=1 in your Where clause?

I'd most likely rewrite it as:


WHERE
(FPCH.ProcessedDateKey >= @Beg_Date AND FPCH.ProcessedDateKey <= @End_Date and 'P' = @DateSolution
or
AR.RunCycleDate>= @pBeg_Date AND AR.RunCycleDate<= @pEnd_Date and 'F' = @DateSolution)
AND FPCH.InventoryStatusCode in (97,98)



- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
al_anderson
al_anderson
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 96
wow, was I over analyzing things! Thanks so much.
GSquared
GSquared
SSC-Dedicated
SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)

Group: General Forum Members
Points: 31507 Visits: 9730
You're welcome.

- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread

"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search