I am trying to use the "Not Equal" command in Sql but it does not seem to be working. I want to get the total count when all the 3 fields (status1, status2 and status3) are not equal to Yes. For some reason I am getting 0 records.
SELECT
COUNT(ID) from [maintable]
WHERE
status1 <> 'YES'
and status2 <> 'YES'
and status3 <> 'YES'
The above query does not generate any results. What am i doing wrong here? Thanks
The SQL Not Equal comparison operator (!=) is used to compare two expressions. For example, 15 != 17 comparison operation uses SQL Not Equal operator (!=) between two expressions 15 and 17.
If != and <> both are the same, which one should be used in SQL queries? Here is the answer – You can use either != or <> both in your queries as both technically same but I prefer to use <> as that is SQL-92 standard.
The <> symbol performs the exact same operation as the != symbol and is used to filter results that do not equal a certain value. You can use either, but <> is the SQL-92 standard.
<> means NOT EQUAL TO, != also means NOT EQUAL TO.
Try this:
SELECT
COUNT(ID) from [maintable]
WHERE
COALESCE(status1, '') <> 'YES'
AND COALESCE(status2, '') <> 'YES'
AND COALESCE(status3, '') <> 'YES'
null
values are not <> 'YES'
, they are undefined.
COALESCE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With