SQLite select where empty?
147,314
Solution 1
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
Solution 2
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
Solution 3
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.
Author by
Timo Huovinen
"The formulation of a problem is often more essential than its solution, which may be merely a matter of mathematical or experimental skill." -Albert Einstein Web Dev that enjoys HTML/CSS, but mainly works with JavaScript, Golang, PHP and SQL. Considers himself to be a beginner forever, even though has experience in the field, the fact that he is self-taught shows with constant beginner questions. Talks about himself in third person. Lacks common sense.
Updated on July 08, 2022Comments
-
Timo Huovinen almost 2 years
In SQLite, how can I select records where
some_column
is empty?
Empty counts as bothNULL
and""
. -
Pacerier over 12 yearsWhat are the advantages of each solution?
-
Guffa over 12 years@Pacerier: There may be some difference in performance, but other than that it's just a matter of style.
-
Pacerier over 12 years@Guffa I mean of course the performance.. This is database isn't it? optimization is important in dbs. a little performance gain is alot
-
peterchen about 12 yearslength(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.
-
Guffa about 12 years@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.