Philip
Philip

Reputation: 2628

CASE Statement in WHERE statement (equal to and not equal to)

I have the following query where I'm trying to return different values from a table based off the values of a bit variable.

Is there a way I can substitute the where condition to get that to work?

DECLARE @isAggregate bit = 0

SELECT 
    *
FROM
    Fields
WHERE
    FieldType  
    CASE 
        WHEN @isAggregate = 1 THEN = 'Aggregate'
        WHEN @isAggregate = 0 THEN <> 'Aggregate'
    END

Upvotes: 1

Views: 967

Answers (1)

Yogesh Sharma
Yogesh Sharma

Reputation: 50173

You can use boolean logic, case expression will not work in this way :

where (@isAggregate = 1 and FieldType = 'Aggregate') or
      (@isAggregate = 0 and FieldType <> 'Aggregate')

Upvotes: 4

Related Questions