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

前端 未结 6 923
夕颜
夕颜 2020-11-30 00:56

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

myObj.someStringField = rdr.IsDBNull(someOrdinal) 
                            ? string.         


        
相关标签:
6条回答
  • 2020-11-30 01:24

    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);}
    
    0 讨论(0)
  • 2020-11-30 01:27

    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:

    • COALESCE() Function (w3schools)
    • SQL As Understood By SQLite (SQLite website)
    0 讨论(0)
  • 2020-11-30 01:28

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

    SELECT myField1
    FROM myTable1
    WHERE myField1 IS NOT NULL
    
    0 讨论(0)
  • 2020-11-30 01:32

    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.

    0 讨论(0)
  • 2020-11-30 01: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).

    0 讨论(0)
  • 2020-11-30 01:40

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

    no brackets around the function

    0 讨论(0)
提交回复
热议问题