Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not Equal does not work in SQL Query

enter image description here

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

like image 865
user1858332 Avatar asked Jan 07 '13 16:01

user1858332


People also ask

Can we use not equal to in SQL query?

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.

Is != The same as <> in SQL?

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.

What is != operator used for in SQL?

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.

What does != Mean in a query?

<> means NOT EQUAL TO, != also means NOT EQUAL TO.


1 Answers

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

like image 113
Tim Schmelter Avatar answered Sep 30 '22 19:09

Tim Schmelter