Home > Software design >  SQL how to express "not equal to and not less than"
SQL how to express "not equal to and not less than"

Time:07-02

I am trying to use a comparison operator in SQL to say where not equal to or less than zero.

I have tried this but it comes up with an error

WHERE amount (<> 0 or < 0)

I have also tried:

WHERE amount !<= 0

but get syntax errors. Any help greatly appreciated

CodePudding user response:

You can simply use:

where amount > 0

CodePudding user response:

With propositional logic, your condition is the same as a != 0:

a != 0 or a < 0

(a < 0 or a > 0) or a < 0

(a < 0) or (a > 0) or (a < 0)

two repeated conditions with or, you can just remove one

(a < 0) or (a > 0)

equivalent to a != 0

aka a <> 0

CodePudding user response:

"not equal to and not less than" <=> "not (equal to or less than)" (see the De Morgan formulas here enter image description here

foo equal 0 to is represented as

foo = 0

foo less than 0 is represented as

foo < 0

foo equal to 0 or foo is less than 0 is represented as

foo = 0 OR foo < 0

this can be shortened to

foo <= 0

its negation is

NOT (foo <= 0)

alternatively, you can use

foo > 0

Note that NULL is a special case and you need to define what you intend NULL to yield. Since that was not specified, I'm focusing only on numeric inputs, but, at the end of the line, you will need to take into account NULL as a possible value as well if... Well, if it's possible in your scenario.

  •  Tags:  
  • sql
  • Related