Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLITE: Select another column if the first one is null

I want to select the value from another column if there are no records in the first column (is null). I want something like this:

SELECT (if Column1 IS NULL THEN Column2), Column3, Column4 FROM MyTable

How can I do that in SQLite?

like image 778
Laura Avatar asked May 30 '13 07:05

Laura


1 Answers

Use the COALESCE function, which returns the first non-null argument:

SELECT COALESCE(Column1, Column2), Column3, Column4
FROM MyTable

In this case, you will get Column1 but if it is NULL, Column2 will be returned instead.

like image 77
lc. Avatar answered Dec 30 '22 01:12

lc.