Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Conditional Where Clause Expand / Collapse
Author
Message
Posted Thursday, July 24, 2008 8:33 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, December 3, 2013 4:46 PM
Points: 14, 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.
Post #540220
Posted Thursday, July 24, 2008 9:47 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 13,872, Visits: 9,596
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
Post #540309
Posted Thursday, July 24, 2008 9:55 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Tuesday, December 3, 2013 4:46 PM
Points: 14, Visits: 96
wow, was I over analyzing things! Thanks so much.
Post #540315
Posted Thursday, July 24, 2008 9:56 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, June 27, 2014 12:43 PM
Points: 13,872, Visits: 9,596
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
Post #540319
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse