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.
For the equivalent of NVL() and ISNULL() use:
IFNULL(column, altValue)
column
: The column you are evaluating.altValue
: The value you want to return if 'column' is null.Example:
SELECT IFNULL(middle_name, 'N/A') FROM person;
*Note: The COALESCE() function works the same as it does for other databases.
Sources:
Try this
e.g
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. Theifnull()
function is equivalent tocoalesce()
with two arguments.If there is not
ISNULL()
method, you can use this expression instead:This works the same as
ISNULL(fieldname, 0)
.Use
IS NULL
orIS NOT NULL
in WHERE-clause instead of ISNULL() method:You can easily define such function and use it then:
or same minified version:
IFNULL
, see here: http://www.sqlite.org/lang_corefunc.html#ifnullno brackets around the function