Link to home
Start Free TrialLog in
Avatar of Jay Roy
Jay RoyFlag for United States of America

asked on

db2 != check

Dear experts

I have a table CUSTOMER with column ID and NAME
Table: CUSTOMER
ID NAME
1  Eric
3  [Null]



I have a simple query to retrieve data where NAME is not 'Eric'

select * from CUSTOMER where NAME != 'Eric'

i am expecting to see
3 [Null]

but it doesnt return anything.
any clue?
Avatar of Ray
Ray
Flag of United States of America image

For grins, try this:
I know they're supposedly both the same thing, but curious if there is some logic that might make one different.

select * from CUSTOMER where NAME <> 'Eric'

Open in new window

Avatar of Jay Roy

ASKER

>>select * from CUSTOMER where NAME <> 'Eric'
nope, already tried that but still doesn't work.
thanks
is the "ID" field an actual entered field or is it an identity field?
Avatar of Jay Roy

ASKER

when I do
select * from CUSTOMER where NAME = 'Eric'
I see one row, so I am really confused why != doesn't work.
Avatar of Jay Roy

ASKER

>>>is the "ID" field an actual entered field or is it an identity field?
ID and NAME are just a VARCHAR fields , no identity field in the table.
ASKER CERTIFIED SOLUTION
Avatar of Ray
Ray
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jay Roy

ASKER

something like this works
select * from CUSTOMER where NAME != 'Eric' or name is null

but is that guaranteed to work is my concern.

I just want to return everything whose NAME is not Eric.
SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The root issue withOUT the "or name is null" is that you can't evaluate null against a condition.  Something either is, or is not, null.  It cannot be compared to any specific value.

In conclusion, if you need the null results you simply have to include that extra bit of code.
Avatar of Member_2_276102
Member_2_276102

A null is neither equal nor not equal. It's... null. That is, a value doesn't exist in that column in that row. If it doesn't exist, a comparison doesn't make logical sense.
Avatar of Jay Roy

ASKER

thanks all.
Hi!

You could do a query like this
select coalesce(NAME , 'A EMPTY FIELD') NAME from CUSTOMER where NAME <> 'Eric' ;

Open in new window


This would give you the names that are in the column NAME excluding the name Eric and
report all rows that are NULL with 'A EMPTY FIELD' string.  
Note that a NULL value indicates that a particular value for a certain column in a certain row is missing.

Regards,
     Tomas Helgi