Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite equivalent to ISNULL(), NVL(), IFNULL() or COALESCE()

I'd like to avoid having many checks like the following in my code:

myObj.someStringField = rdr.IsDBNull(someOrdinal)                              ? string.Empty                              : rdr.GetString(someOrdinal); 

I figured I could just have my query take care of the nulls by doing something like this:

SELECT myField1, [isnull](myField1, '')  FROM myTable1 WHERE myField1 = someCondition 

I'm using SQLite though and it doesn't seem to recognize the isnull function. I've also tried some equivalent ones recognized in other databases (NVL(), IFNULL() and COALESCE()), but SQLite doesn't seem to recognize any of them.

Does anyone have any suggestions or know of a better way to do this. Unfortunately the database doesn't have default values for all fields. Plus, I need to use some LEFT JOIN clauses in some cases, where some of the fields returned will be null because the matching record in the LEFT JOIN table will not exist.

like image 632
Jason Down Avatar asked Apr 28 '09 18:04

Jason Down


People also ask

Is NVL same as Isnull?

ISNULL replaced the Oracle NVL function in the SQL server. When an expression in SQL server is NULL, the ISNULL function allows you to return an alternative value for the null. ISNULL checks whether the value or an expression is true or false.

Does coalesce work in SQLite?

How Coalesce() Works in SQLite. In SQLite, the coalesce() function returns a copy of its first non-NULL argument, or NULL if all arguments are NULL. It's similar to the ifnull() function, except that coalesce() accepts more than two arguments ( ifnull() only accepts two arguments).

Does SQLite support Isnull?

The ifnull function can be used in the following versions of SQLite: SQLite 3.8. 6, SQLite 3.8. x, SQLite 3.7.

Is coalesce same as Ifnull?

IFNULL checks a single argument. COALESCE works with N arguments. COALESCE is useful when you have unknown number of values that you want to check. IFNULL is useful when you select columns and know that it can be null but you want to represent it with a different value.


2 Answers

IFNULL, see here: http://www.sqlite.org/lang_corefunc.html#ifnull

no brackets around the function

like image 83
SQLMenace Avatar answered Sep 21 '22 16:09

SQLMenace


Try this

ifnull(X,Y)   

e.g

select ifnull(InfoDetail,'') InfoDetail; -- this will replace null with '' select ifnull(NULL,'THIS IS NULL');-- More clearly.... 

The ifnull() function returns a copy of its first non-NULL argument, or NULL if both arguments are NULL. Ifnull() must have exactly 2 arguments. The ifnull() function is equivalent to coalesce() with two arguments.

like image 44
Hardik Darji Avatar answered Sep 20 '22 16:09

Hardik Darji