Conditional Where Clause

  • 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.

  • 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

  • wow, was I over analyzing things! Thanks so much.

  • 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

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply