CASE Expression in WHERE

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adobe
    New Member
    • Mar 2010
    • 1

    CASE Expression in WHERE

    I know you should be able to use a Case Expression within a WHERE Clause but I am having some trouble.

    The user will be able to select what type of Operator they want so they can search client balance information...b ecause of this I need 1 clause for the BETWEEN and one for all the others (>=, =, <= , ect). I am receiving error at line 7 "Incorrect syntax near the keword THEN which doesn't help much.

    DECLARE @Operator VARCHAR(10)
    SET @Operator = '>'

    SELECT Balance
    FROM ClientInformati on
    WHERE @Operator = CASE WHEN 'BETWEEN'
    THEN CAST(Balance AS MONEY) BETWEEN 1 and 2
    ELSE CAST(Balance AS MONEY) @Operator 30
    END

    Can someone help me out here
  • ck9663
    Recognized Expert Specialist
    • Jun 2007
    • 2878

    #2
    Try something like this:

    Code:
    WHERE (@Operator = 'BETWEEN' and CAST(Balance AS MONEY) BETWEEN 1 and 2) or
     (@Operator = '>'and CAST(Balance AS MONEY) > 30
    ) or
    (@Operator = '<'and CAST(Balance AS MONEY) < 30
    )
    Happy Coding!!!

    ~~ CK

    Comment

    Working...