Using multiple conditions in where clause of SQL Server

sql multiple conditions same column
sql multiple where clauses in one query
sql where multiple conditions
sql two different where conditions for two columns
sql query for multiple values in single field
sql where clause
sql having multiple conditions
sql server in clause

I have a table called finalres in my database which has list of statuses and accounts.

I want to pull the accounts where the status should not be in:

(xxx, ina, nfc)

Also I want to pull the accounts where the status in RWD but only when account# is null. I wrote the below query but it only giving result for either one condition. please help me.

select *
from finalres
where 1 = 0
   or (status = 'rwd' and account# is null)
   or status not in ('xxx', 'ina', 'nfc')
select * from finalres where 
(status='rwd' and account# is null) 
 or  status not in ('xxx','ina','nfc')

You can check this query at link below:

http://sqlfiddle.com/#!18/11b3d/2

 CREATE TABLE finalres
(
  [account] int,
  [ItemNo] varchar(32),
  status varchar(100)

) 

INSERT INTO finalres (account, ItemNo, status) VALUES
  ('1', '453', 'xxx'),
  ('2', '657', '34'),
  (null, '657', 'rwd')
  ;


account     ItemNo  status
2            657     34
(null)       657     rwd

Using multiple conditions in where clause of SQL Server, In the Filter column for the second data column, specify the second condition. The Query and View Designer creates a WHERE clause that  SQL - Multiple Conditions; using the AND and OR conditions: In the previous article, Using the WHERE clause with the SQL SELECT Statement, we demonstrated how you would use the SQL WHERE clause to conditionally select data from the database table. The WHERE clause can be simple and use only a

You have your list of statuses (xxx, ina, nfc) that you do not want records with. In addition, you only want records with a status of RWD when the account# is null, which means you need to add that status to you list of statuses that you do not want. That give you a query like this:

select
    *
from
    finalres
where
     status not in ('rwd','xxx','ina','nfc')
  or (status='rwd' and account is null)

SQL: Combining the AND and OR Conditions, Learn how to use SQL's SELECT statement. You can specify multiple conditions in a single WHERE clause to, say, retrieve rows based on  What I'm trying to do is use more than one CASE WHEN condition for the same column. Here is my code for the query: SELECT Url='', p.ArtNo, p. [Description], p.Specification, CASE WHEN 1 = 1 or 1 = 1 THEN 1 ELSE 0 END as Qty, p.NetPrice, [Status] = 0 FROM Product p (NOLOCK) However, what I want to do is use more then one WHEN for the same column

The problem is the status not in ('xxx','ina','nfc') allows the result to include any status='rwd', even if the account# is not null. This makes the (status='rwd' and account# is null) redundant. You will need to include the 'rwd' in the status not in query.

select 
*
from finalres
where 1 = 0
or (status='rwd' and account# is null)
or status not in ('rwd','xxx','ina','nfc')

Specify Multiple Search Conditions for Multiple Columns, The AND operator allows the existence of multiple conditions in an SQL statement's WHERE clause. Syntax. The basic syntax of the AND operator with a WHERE  If you are using parametrized Stored procedure: Pass in comma separated string. Use special function to split comma separated string into table value variable. Use INNER JOIN ON t.PersonName = newTable.PersonName using a table variable which contains passed in names.

Try this,

    select * 
      from finalres 
     where (status='rwd' and account# is null) 
        or status not in ('xxx','ina','nfc')

Combining and Negating Conditions with AND, OR, and NOT, SQL - Multiple Conditions; using the AND and OR conditions: In the previous The WHERE clause can be simple and use only a single condition (like the one If we include multiple operators in the query, SQL Server evaluates them in the​  Browse other questions tagged sql sql-server operators where-clause or ask your own question. Blog Podcast: A chat with our CEO about the future of our company and community

SQL - AND and OR Conjunctive Operators, The precedence for Boolean operators is – AND comes first, the OR and then NOT. It is good practice to use parenthesis if using multiple Boolean  To answer the underlying question of how to use a CASE expression in the WHERE clause: First remember that the value of a CASE expression has to have a normal data type value, not a boolean value. It has to be a varchar, or an int, or something.

SQL AND and OR conditions, The IN operator allows multiple values to be tested against the expression and thus reduces the use of multiple OR conditions with each test value. Syntax: In the WHERE clause, you specify a search condition to filter rows returned by the FROM clause. The WHERE clause only returns the rows that cause the search condition to evaluate to TRUE. The search condition is a logical expression or a combination of multiple logical expressions. In SQL, a logical expression is often called a predicate.

SQL Server – SELECT Statement multiple WHERE conditions , This example uses the WHERE clause to define multiple conditions, but instead of using the AND condition, it uses the OR condition. In this case, this SQL statement would return all records from the products table where the product_name is either Pear or Apple.

Comments
  • Please edit your question to show example data (both source data and query results) that demonstrates both what you want to happen and what's going wrong with your current query.
  • what do you mean by 1 = 0 in your where clause ?
  • @Farshad So that developers can add conditions after and/or easily, instead of spending time to find where to add where statement. It's just for the convenience.
  • it still not working.it works only the first condition
  • @unnikrishnan can you please put some data of your table
  • If status = 'rwd' then status not in ('xxx','ina','nfc') is redundant. That's like saying x=4 AND x NOT IN (1,2,3,5,6,7), you already know x is 4, so it can't be any of the other values.
  • (X AND Y) AND Z is functionally identical to X AND Y AND Z is functionally identical to X AND (Y AND Z) is functionally identical to (X AND Z) AND Y, etc, etc, etc, etc.