SQL: Can I negate a condition in a where clause?

conditional-statements, sql, where-clause

Solution

SQL's equivalent of `!` in C is `NOT`. However, in your case you want something else: you need to build a condition that decides between the two choices based on the value of `@checkbool`, like this:

SELECT *
FROM TableA A
WHERE (    (@checkbool) AND (A.Id =  123))
   OR ((NOT @checkbool) AND (A.Id <> 123))

Problem

I want to check if a boolean is true, then decide in the WHERE clause what condition to use. Say the boolean variable is @checkbool: ``` SELECT * FROM TableA A WHERE --if @checkbool is true, run this A.Id = 123 --if @checkbool is false, run this A.Id <> 123 ``` Is there a way to negate a condition? Like in C++ you can do if !(condition). If not, what is the best way to solve this problem? Thank you!

Original source