144

In SQLite, how can I select records where some_column is empty?
Empty counts as both NULL and "".

jkoop
  • 113
  • 1
  • 7
Timo Huovinen
  • 53,325
  • 33
  • 152
  • 143

4 Answers4

319

There are several ways, like:

where some_column is null or some_column = ''

or

where ifnull(some_column, '') = ''

or

where coalesce(some_column, '') = ''

of

where ifnull(length(some_column), 0) = 0
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • What are the advantages of each solution? – Pacerier Oct 15 '11 at 14:54
  • 1
    @Pacerier: There may be some difference in performance, but other than that it's just a matter of style. – Guffa Oct 15 '11 at 16:21
  • 6
    @Guffa I mean of course the performance.. This is database isn't it? optimization is important in dbs. a little performance gain is **alot** – Pacerier Oct 15 '11 at 16:29
  • 6
    length(some_column) should be avoided, since this may calculate the length on the fly - AFAIK current SQLite does for columns with text affinity. Other than that, you are at the mercy of the optimizer - though I would expect them to be identical. You could verify that by using EXPLAIN. – peterchen Apr 16 '12 at 11:04
  • @peterchen: Yes, it depends on what the optimiser does. I included the `length` example because it might actually be faster in some situation, as comparing numbers is simpler than comparing strings. If the performance for that is a concern, you should of course check what it does. – Guffa Apr 16 '12 at 11:16
31

It looks like you can simply do:

SELECT * FROM your_table WHERE some_column IS NULL OR some_column = '';

Test case:

CREATE TABLE your_table (id int, some_column varchar(10));

INSERT INTO your_table VALUES (1, NULL);
INSERT INTO your_table VALUES (2, '');
INSERT INTO your_table VALUES (3, 'test');
INSERT INTO your_table VALUES (4, 'another test');
INSERT INTO your_table VALUES (5, NULL);

Result:

SELECT id FROM your_table WHERE some_column IS NULL OR some_column = '';

id        
----------
1         
2         
5    
Daniel Vassallo
  • 337,827
  • 72
  • 505
  • 443
1

Maybe you mean

select x
from some_table
where some_column is null or some_column = ''

but I can't tell since you didn't really ask a question.

µBio
  • 10,668
  • 6
  • 38
  • 56
0

You can do this with the following:

int counter = 0;
String sql = "SELECT projectName,Owner " + "FROM Project WHERE Owner= ?";
PreparedStatement prep = conn.prepareStatement(sql);
prep.setString(1, "");
ResultSet rs = prep.executeQuery();
while (rs.next()) {
    counter++;
}
System.out.println(counter);

This will give you the no of rows where the column value is null or blank.

tobi6
  • 8,033
  • 6
  • 26
  • 41