Arianule
Arianule

Reputation: 9043

selecting data where column='null'

I want to query data and the column (Continent) either has a value of 'Africa' or Null.

I want to use this column name in my query but for the countries where the name is not Africa I get no data.

This is what I have tried but returns no data.

SqlDataAdapter dba = new SqlDataAdapter(@"SELECT * from [tblCountries] 
                     WHERE [Continent] = '' order by [Common Name]", connection);
                    //WHERE [Continent]<>'Africa' order by [Common Name]", connection);
                   //WHERE [Continent] IS null order by [Common Name]", connection);
dba.Fill(ds);

drpNonAfricanCountries.DataSource = ds;

What would be the correct way of doing this?

Upvotes: 0

Views: 91

Answers (4)

kavita verma
kavita verma

Reputation: 75

try this:

SELECT * from [tblCountries] WHERE [Continent] IS NULL OR [Continent]= 'Africa' order by [Common Name];

Upvotes: 1

Qasim Javaid Khan
Qasim Javaid Khan

Reputation: 660

select * from tblCountries where (Continent = 'Africa' or Continent is null)

Upvotes: 1

Rajeev Kumar
Rajeev Kumar

Reputation: 4963

Try like this

SqlDataAdapter dba = new SqlDataAdapter(@"SELECT * from [tblCountries] 
                                     WHERE [Continent] IS NULL order by [Common Name]", connection);
            dba.Fill(ds);
            drpNonAfricanCountries.DataSource = ds;

Upvotes: 1

Habib
Habib

Reputation: 223267

I want to query data and the column (Continent) either has a value of 'Africa' or Null.

WHERE [Continent] = 'Africa' OR  [Continent] IS NULL

For comparing a column with NULL use IS NULL

So your final code should be:

SqlDataAdapter dba = new SqlDataAdapter(@"SELECT * from [tblCountries] 
               WHERE [Continent] = 'Africa' OR  [Continent] IS NULL order by [Common Name]", connection);

Upvotes: 3

Related Questions