SQLite equivalent to ISNULL(), NVL(), IFNULL() or COALESCE()
Asked Answered
A

6

112

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.

Achaean answered 28/4, 2009 at 18:57 Comment(0)
X
155

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

no brackets around the function

Xenocrates answered 28/4, 2009 at 19:3 Comment(2)
Bah it was the square brackets. Thanks for that. Was driving me crazy that the documentation said it was supported (also has coalesce in there), but it wasn't working. One of those days...Achaean
I realize your description of "no brackets around the function" references the question, but with a statement like that, it would help to have an example underneath your initial statement.Thallophyte
S
56

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.

Solent answered 24/6, 2013 at 11:50 Comment(1)
Yes, I figured out that it was ifnull I wanted... I just needed to drop the square brackets I was using.Achaean
T
32

If there is not ISNULL() method, you can use this expression instead:

CASE WHEN fieldname IS NULL THEN 0 ELSE fieldname END

This works the same as ISNULL(fieldname, 0).

Terina answered 19/12, 2010 at 12:45 Comment(4)
The function ifnull is the SQLite equivalent of the isnull function the question was asking about. To anyone reading this now, please see SQLMenace's answer (from over a year and a half earlier nevertheless) before writing your own solution using CASE.Disappearance
@Disappearance - You make a good point. However, this answer is helpful for those potentially using sqlite just for unit testing, and using a different RDBMS for live code. In that case, using something like CASE statements might make more sense than IFNULL.Plumbo
@sethflowers I can see the value in implementing null default values in a more RDBMS-neutral way but that is the answer to a different question. :) The OP is simply asking for SQLite's equivalent to isnull in other systems and I wouldn't want to encourage people to "roll their own."Disappearance
I was just looking for something like an ifnotnull() or ifnonnull() to complement ifnull(), for use in string concatenation within the SELECT clause to display "last_name, first_name" but only concatenate the comma if first_name is non-null. This approach let me do that.Hydrastis
L
9

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:

Lyophilize answered 11/10, 2018 at 20:56 Comment(0)
A
6

Use IS NULL or IS NOT NULL in WHERE-clause instead of ISNULL() method:

SELECT myField1
FROM myTable1
WHERE myField1 IS NOT NULL
Affenpinscher answered 6/7, 2016 at 7:24 Comment(2)
Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this".Fealty
That's not relevant to what was asked. The OP doesn't want to filter out rows where myField1 has a non-null value, he wants to replace the value in the result column with a different one, if the row value is null.Fisticuffs
M
-6

You can easily define such function and use it then:

ifnull <- function(x,y) {
  if(is.na(x)==TRUE) 
    return (y)
  else 
    return (x);
}

or same minified version:

ifnull <- function(x,y) {if(is.na(x)==TRUE) return (y) else return (x);}
Manuelmanuela answered 18/4, 2015 at 4:38 Comment(1)
That's not SQLite.Fisticuffs

© 2022 - 2024 — McMap. All rights reserved.