How to select data from MySQL table where column is NULL

I expect the following code to show me all the records in the table where the exchange column is zero, but the result set shows 0 rows. Any idea why?

SELECT * FROM pubco WHERE exchange IS NULL; 
+2
sql mysql select
Jan 11 '13 at 14:14
source share
4 answers

You may have interpreted '' as NULL , which is not the same, but try this

 SELECT * FROM pubco WHERE exchange IS NULL OR exchange = '' 

but if you still don't get the value, maybe there are spaces on it, so you should TRIM it,

 SELECT * FROM pubco WHERE exchange IS NULL OR TRIM(exchange) = '' 
+4
Jan 11 '13 at 14:16
source share

One important note: NULL and '' (i.e. an empty row) are not the same thing, most likely your column contains empty rows, so you need to add another condition:

 SELECT * FROM pubco WHERE (exchange IS NULL OR exchange = ''); 
0
Jan 11 '13 at 14:17
source share

Are you sure they are zero?

Perhaps they have a string value of "NULL", this is not the same as what it returns if you do:

 SELECT * FROM pubco WHERE exchange == "NULL"; 
0
Jan 11 '13 at 14:17
source share

Instead of using this query

 SELECT * FROM pubco WHERE exchange IS NULL OR exchange = '' 

try it

 SELECT * FROM pubco WHERE isnull(exchange,'')=''; 

This will improve performance. .

0
Jan 11 '13 at 14:24
source share



All Articles