Where Clause in T-SQL

  • Hello,

    I am trying to use following conditions in my where clause but trying make sure that i ma using correct way.

    Requirement: need to change, see below:

    a. Program <> 35 and acct1 <> 1

    b. Program = 30, 31 and acct1 =6

    My Where clause is correct as below?

    WHERE TYPE <> 35 AND ACCT1 <> 2

    AND TYPE IN (30, 31) AND ACCT1 = 6

    Is it i am using correct way or i need to modify it to?

     

    Thanks for your help!

     

  • That looks OK to me. Does it work?

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • That requirement looks suspect to me.

    Do (a) and (b) both have to be true (AND), or is it a case of one or the other (OR)?

    If you haven't even tried to resolve your issue, please don't expect the hard-working volunteers here to waste their time providing links to answers which you could easily have found yourself.

  • Looks like it should be:

    WHERE (TYPE <> 35 AND ACCT1 <> 2)
    OR (TYPE IN (30, 31) AND ACCT1 = 6)
  • The way you have it written you don't need the "TYPE <> 35 AND ACCT1 <> 2".  The check where the Type IN (30, 31) will insure you don't get type 35 the same can be said for the check for Acct1 if it has to be 6 then the check for <> 2 isn't needed.

    This should be all you need:

    WHERE Type IN (30,31) AND Acct1 = 6

    Unless like others have suggested that this should be one OR the other, then I would go with the code that pietlinden posted.

    -------------------------------------------------------------
    we travel not to escape life but for life not to escape us
    Don't fear failure, fear regret.

  • I agree with Phil and pietlinden, the only way those criteria make sense is if you evaluate these separately with an OR between them.

  • Thanks.

    I am greed with you guys as checking with user exact condition. Both condition is overlapping so it's should be only one.

  • Thanks everyone for your response.

Viewing 8 posts - 1 through 7 (of 7 total)

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