Null substitution in SQLite: Null substitution in SQLite: sqlite sqlite

Null substitution in SQLite:


You should use the standard COALESCE function:

select coalesce(MyColumn, -1) as MyColumn from MyTable

Any database that understands standard ANSI SQL will support COALESCE so using it is a good habit to get into.


You can use ifnull:

select ifnull(MyColumn,-1) as MyColumn from MyTable


The SQL standard way to do this is with CASE:

 SELECT CASE WHEN MyColumn IS NULL THEN -1 ELSE MyColumn END FROM MyTable

Considerably more verbose than engine-specific IFNULL, ISNULL, NZ functions, but more portable.

(Or, as mu points out, you can use the much better COALESCE for this).