Simon Hume
Simon Hume

Reputation: 1012

Filtering out NULL entries in a SQLite database

I'm using SQLite to retrieve data inside an iOS app I'm developing.

Of the dataset i've been given by the client there are a lot of entries than contain columns with NULL values.

I want to make sure I filter these out so they aren't shown in the app.

I've tried all sorts of different syntax combinations, with no joy, they still keep appearing.

This is my current SQL

select * from bn_main order by RANDOM() LIMIT 1

I need it to look in the 'meaning' column and perform a WHERE to identify and therefore remove any entries that have a NULL value in that column.

Anyone able to help?

Upvotes: 3

Views: 5334

Answers (1)

mechanical_meat
mechanical_meat

Reputation: 169284

NULL is a special case.
That means you can't do something like WHERE meaning <> NULL.
Instead you would use IS / IS NOT, e.g.:

  select meaning 
    from bn_main 
   where meaning IS NOT NULL
order by RANDOM() LIMIT 1;

Edit based on comment:
If you've tried this and are still getting results, the values to which you refer are not NULLs.
Instead filter out columns containing whitespace:

  select meaning 
    from bn_main 
   where TRIM(meaning) <> ''
order by RANDOM() LIMIT 1;

Upvotes: 7

Related Questions