Help with select please

  • I want to select records based on a parameter. Parameter is 0 or 1. When parameter is 1 I want to include all records and when the parameter is 0 I only want to include records where an integer column that could contain 0, 1, or 2 is 0 or 1.

    Seems like I should be able to do this in a WHERE clause but I dont know tsql well enough. Would I use a CASE?

    Struggling.... 🙁

    thanks,

    vmo

  • This should work:

    Where field Between 0

    And

    (Case

    When parameter = 0 Then 1

    Else 2

    End)

  • Most excellent. I learned something new today.

    Thank you.

  • another option is

    ... where field < (2 + parameter)

    the downside to using this type of calculation is

    * the logic isn't as straight-forward. So other folks looking at the code may end up puzzled

    * if your business logic changes, the calculation may need a bigger change than using the CASE.

    Kurt

  • or remove the case all together...

    SELECT * FROM yourTable WHERE yourField <= @parameter + 1

    ______________________________________________________________________

    Personal Motto: Why push the envelope when you can just open it?

    If you follow the direction given HERE[/url] you'll likely increase the number and quality of responses you get to your question.

    Jason L. Selburg

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

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