Liam neesan
Liam neesan

Reputation: 2551

How to set AND condition is for all to other OR Condition in SQL Server?

I want to set the condition using AND and OR in WHERE clause. But the problem is AND condition is not setting to all other OR Condition

ex:

SELECT
    Item,
    Name,
    Brand,
    Date,
    StoreID
FROM
    Item
WHERE
    Date between '01-01-2017' and '30-01-2017' 
    OR Brand = 'Nike' or Brand = 'Jhonson' OR Brand = 'Polo'

In my scenario I cannot use IN to select multiple Brand. So I want to set all Date filter to all the brands. How can I achieve it.

Thanks

Upvotes: 0

Views: 62

Answers (3)

Rohit Poudel
Rohit Poudel

Reputation: 1889

We have two Scenario here as follows

  SELECT
        Item,
        Name,
        Brand,
        Date,
        StoreID
    FROM
        Item
    WHERE
        Date between '01-01-2017' and '30-01-2017' 
        OR (Brand = 'Nike' or Brand = 'Jhonson' OR Brand = 'Polo')


SELECT
    Item,
    Name,
    Brand,
    Date,
    StoreID
FROM
    Item
WHERE
    Date between '01-01-2017' and '30-01-2017' 
    AND (Brand = 'Nike' or Brand = 'Jhonson' OR Brand = 'Polo')

Upvotes: 1

Tien Nguyen Ngoc
Tien Nguyen Ngoc

Reputation: 1555

SELECT
    Item,
    Name,
    Brand,
    Date,
    StoreID
FROM
    Item
WHERE
    Date between '01-01-2017' and '30-01-2017' 
    AND ( Brand = 'Nike' or Brand = 'Jhonson' OR Brand = 'Polo')

It can be right with your result.

Upvotes: 0

dbajtr
dbajtr

Reputation: 2044

This would give you the all the results that match both clauses

WHERE
    (Date between '01-01-2017' and '30-01-2017') 
     AND 
    (Brand IN ('Nike','Jhonson','Polo'))

This would give you everything that matches any of the clauses

WHERE
    (Date between '01-01-2017' and '30-01-2017') 
     OR 
    (Brand IN ('Nike','Jhonson','Polo'))

Upvotes: 1

Related Questions